{"id":3253,"date":"2020-09-12T06:34:27","date_gmt":"2020-09-11T21:34:27","guid":{"rendered":"https:\/\/taitan916.info\/blog\/?p=3253"},"modified":"2024-01-16T17:44:12","modified_gmt":"2024-01-16T08:44:12","slug":"post-3253","status":"publish","type":"post","link":"https:\/\/taitan916.info\/blog\/archives\/3253","title":{"rendered":"MySQL\u3067time\u578b\u30c7\u30fc\u30bf\u304b\u3089int\u578b\u30c7\u30fc\u30bf\u306e\u6e1b\u7b97\u3092\u884c\u3044\u3001\u6e1b\u7b97\u5f8c\u306e\u30c7\u30fc\u30bf\u3067\u6761\u4ef6\u691c\u7d22\u3059\u308b\u65b9\u6cd5"},"content":{"rendered":"<p>MySQL\u3067time\u578b\u30c7\u30fc\u30bf\u304b\u3089int\u578b\u30c7\u30fc\u30bf\u306e\u6e1b\u7b97\u3092\u884c\u3044\u3001\u6e1b\u7b97\u5f8c\u306e\u30c7\u30fc\u30bf\u3092\u691c\u7d22\u6761\u4ef6\u306b\u542b\u3081\u305f\u3044\u3068\u3044\u3063\u305f\u30b1\u30fc\u30b9\u304c\u3042\u3063\u305f\u3002\u5177\u4f53\u4f8b\u3001\u5bfe\u5fdc\u7b56\u3092\u4ee5\u4e0b\u306b\u30e1\u30e2\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u5177\u4f53\u4f8b<\/h2>\n<p>\u98f2\u98df\u5e97\u306e\u60c5\u5831\u3092MySQL\u3067\u53d6\u308a\u6271\u3046\u6848\u4ef6\u3067\u55b6\u696d\u958b\u59cb\u6642\u9593(time\u578b)\u3001\u55b6\u696d\u7d42\u4e86\u6642\u9593(time\u578b)\u3001\u55b6\u696d\u7d42\u4e86\u304b\u3089\u25cb\u5206\u524d\u306b\u6ce8\u6587\u53d7\u4ed8\u7d42\u4e86\u306b\u306a\u308b\u30e9\u30b9\u30c8\u30aa\u30fc\u30c0\u30fc(int\u578b)\u306e\u30c7\u30fc\u30bf\u3092\u683c\u7d0d\u3057\u305f\u3002\u4ee5\u4e0b\u304c\u30b5\u30f3\u30d7\u30eb\u3068\u306a\u308b\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">mysql&gt; show columns from shop;\r\n+------------+------------------+------+-----+---------+----------------+\r\n| Field      | Type             | Null | Key | Default | Extra          |\r\n+------------+------------------+------+-----+---------+----------------+\r\n| id         | int(11)          | NO   | PRI | NULL    | auto_increment |\r\n| name       | varchar(255)     | NO   |     | NULL    |                |\r\n| start_time | time             | YES  |     | NULL    |                |\r\n| end_time   | time             | YES  |     | NULL    |                |\r\n| last_order | int(11) unsigned | NO   |     | NULL    |                |\r\n+------------+------------------+------+-----+---------+----------------+\r\n\r\nmysql&gt; select * from shop;\r\n+----+-----------+------------+----------+------------+\r\n| id | name      | start_time | end_time | last_order |\r\n+----+-----------+------------+----------+------------+\r\n|  1 | \u725b\u89d2      | 10:00:00   | 22:00:00 |         30 |\r\n|  2 | \u53d9\u3005\u82d1    | 09:00:00   | 19:00:00 |         15 |\r\n+----+-----------+------------+----------+------------+<\/pre>\n<p>&nbsp;<\/p>\n<h2>\u55b6\u696d\u4e2d\u306e\u5e97\u8217\u306e\u307f\u8868\u793a<\/h2>\n<p>\u55b6\u696d\u4e2d\u306e\u5e97\u8217\u306e\u307f\u8868\u793a\u3055\u305b\u305f\u3044\u5834\u5408\u3001\u3088\u304f\u3042\u308b\u30d1\u30bf\u30fc\u30f3\u3060\u3068\u4ee5\u4e0b\u306b\u306a\u308b\u304b\u3068\u601d\u308f\u308c\u308b\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">SELECT * \r\nFROM shop \r\nWHERE start_time &lt;= now() \r\nAND end_time &gt;= now();<\/pre>\n<p>\u305f\u3060\u3001\u4eca\u56de\u306f\u55b6\u696d\u7d42\u4e86\u6642\u9593\u3067\u306f\u306a\u304f\u6ce8\u6587\u53d7\u4ed8\u7d42\u4e86\u6642\u306e\u6642\u9593\u3092\u8ef8\u306b\u3057\u305f\u304b\u3063\u305f\u3002\u3064\u307e\u308a\u300c\u55b6\u696d\u7d42\u4e86\u6642\u9593 - \u30e9\u30b9\u30c8\u30aa\u30fc\u30c0\u30fc\u306e\u5206\u6570\u300d\u3092\u62bd\u51fa\u3057\u3001\u305d\u306e\u5024\u3092\u691c\u7d22\u6761\u4ef6\u306b\u542b\u3081\u305f\u3044\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>time\u578b\u30c7\u30fc\u30bf\u306e\u6e1b\u7b97<\/h2>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\/\/MySQL5.1\u306e\u5834\u5408\r\nSELECT \r\n    *, \r\n    SEC_TO_TIME (\r\n        TIME_TO_SEC(end_time) - ( last_order * 60)\r\n    ) AS last_time \r\nFROM shop;\r\n\r\n\/\/MySQL5.7\u306e\u5834\u5408\r\nSELECT \r\n    *, \r\n    ( end_time - INTERVAL last_order MINUTE ) AS last_time \r\nFROM shop;<\/pre>\n<p>\u4e0a\u8a18SQL\u6587\u3092\u6253\u3064\u3068last_time\u30ab\u30e9\u30e0\u306b\u6e1b\u7b97\u5f8c\u306e\u6642\u9593\u304c\u5165\u308b\u3002<\/p>\n<p>\u5c1a\u3001MySQL\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u306b\u3088\u3063\u3066\u5bfe\u5fdc\u65b9\u6cd5\u304c\u7570\u306a\u308b\u3063\u307d\u3044\u3002ver5.7\u306e\u8a18\u8ff0\u30925.1\u3067\u884c\u3063\u3066\u3082NULL\u304c\u8fd4\u3063\u3066\u304f\u308b\u306e\u3067\u6ce8\u610f\u3002<\/p>\n<p>5.1\u3060\u3068end_time\u3068last_order\u3092\u79d2\u6570\u306b\u5909\u63db\u3057\u305f\u5f8c\u306b\u6e1b\u7b97\u3057\u3001\u305d\u306e\u7d50\u679c\u3092time\u578b\u306b\u5909\u63db\u3057\u306a\u304a\u3059\u3068\u3044\u3063\u305f\u975e\u5e38\u306b\u9762\u5012\u304f\u3055\u3044\u624b\u9806\u3068\u306a\u308b\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u6e1b\u7b97\u5f8c\u306e\u30c7\u30fc\u30bf\u3092\u691c\u7d22\u6761\u4ef6\u306b\u542b\u3081\u308b<\/h2>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\/\/MySQL5.1\u306e\u5834\u5408\r\nSELECT \r\n    *, \r\n    SEC_TO_TIME (\r\n        TIME_TO_SEC(end_time) - ( last_order * 60)\r\n    ) AS last_time,\r\n    IF(\r\n        start_time &lt;= now() &amp;&amp;\r\n        SEC_TO_TIME (\r\n            TIME_TO_SEC(end_time) - ( last_order * 60)\r\n        ) &gt;= now(), 1, 0\r\n    ) AS open_flg\r\nFROM shop\r\nHAVING open_flg = 1;\r\n\r\n\/\/MySQL5.7\u306e\u5834\u5408\r\nSELECT \r\n    *, \r\n    ( end_time - INTERVAL last_order MINUTE ) AS last_time, \r\n    IF(\r\n        start_time &lt;= now() &amp;&amp;\r\n        ( end_time - INTERVAL last_order MINUTE ) &gt;= now(), 1, 0\r\n    ) AS open_flg\r\nFROM shop\r\nHAVING open_flg = 1;<\/pre>\n<p>\u300c\u73fe\u5728\u6642\u523b\u304cstart_time\u3088\u308a\u5927\u304d\u3044(\u55b6\u696d\u958b\u59cb\u6642\u9593\u3092\u904e\u304e\u3066\u3044\u308b)\u300d\u304b\u3064\u300c\u73fe\u5728\u6642\u523b\u304clast_time\u3088\u308a\u5c0f\u3055\u3044(\u6ce8\u6587\u53d7\u4ed8\u7d42\u4e86\u6642\u9593\u524d)\u300d\u306e\u5834\u5408\u306fopen_flg\u30ab\u30e9\u30e0\u306f1\u3001\u305d\u308c\u4ee5\u5916\u306e\u5834\u5408\u306f0\u3068\u306a\u308b\u3002<\/p>\n<p>\u5c1a\u4e14\u3064HAVING\u3067open_flg\u304c1\u306e\u3082\u306e\u3092\u62bd\u51fa\u3057\u3066\u3044\u308b\u3002WHERE\u3060\u3068\u62bd\u51fa\u3067\u304d\u306a\u3044\u306e\u3067\u6ce8\u610f\u3059\u308b\u3002IF~\u306e\u53c2\u8003\u6f14\u7b97\u5b50\u306e\u90e8\u5206\u306f<a href=\"https:\/\/taitan916.info\/blog\/archives\/3202#IFgroup_id_ltgt_group_id_no1_nono1_AS_no\">\u904e\u53bb\u8a18\u4e8b<\/a>\u53c2\u8003\u3002<\/p>\n<p>\u5c1a\u3001HAVING\u3092\u3064\u3051\u305a\u306bORDER BY\u306bopen_flg\u3092\u6307\u5b9a\u3059\u308b\u3053\u3068\u3067\u55b6\u696d\u6642\u9593\u4e2d\u306e\u3082\u306e\u3092\u512a\u5148\u8868\u793a\u3001\u3068\u3044\u3063\u305f\u3053\u3068\u3082\u5bfe\u5fdc\u304c\u53ef\u80fd\u3068\u306a\u308b\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u6240\u898b<\/h2>\n<p>MySQL\u3067if\u69cb\u6587\u3092\u4f7f\u3046\u7d4c\u9a13\u304c\u307b\u3068\u3093\u3069\u7121\u304b\u3063\u305f\u306e\u3067\u300c\u30b5\u30d6\u30af\u30a8\u30ea\u3067\u306a\u3093\u3068\u304b\u306a\u3089\u306a\u3044\u304b\u300d\u300c\u9762\u5012\u304f\u3055\u3044\u304b\u3089PHP\u5074\u3067\u51e6\u7406\u3057\u3061\u3083\u304a\u3046\u304b\u300d\u3068\u7d06\u4f59\u66f2\u6298\u3057\u3001\u89e3\u6c7a\u307e\u3067\u975e\u5e38\u306b\u6642\u9593\u304c\u304b\u304b\u3063\u305f\u3002\u4eca\u5f8c\u306b\u6d3b\u304b\u3057\u305f\u3044\u3068\u306f\u601d\u3046\u304c\u96e3\u89e3\u306aSQL\u6587\u3092\u4f5c\u6210\u3059\u308b\u30b1\u30fc\u30b9\u304c\u3042\u307e\u308a\u306a\u3044\u306e\u3067\u305d\u306e\u8fba\u306f\u96e3\u3057\u3044\u3068\u3053\u308d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MySQL\u3067time\u578b\u30c7\u30fc\u30bf\u304b\u3089int\u578b\u30c7\u30fc\u30bf\u306e\u6e1b\u7b97\u3092\u884c\u3044\u3001\u6e1b\u7b97\u5f8c\u306e\u30c7\u30fc\u30bf\u3092\u691c\u7d22 ... <\/p>\n","protected":false},"author":1,"featured_media":1072,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[5],"tags":[],"class_list":["post-3253","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mysql"],"acf":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/posts\/3253","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/comments?post=3253"}],"version-history":[{"count":0,"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/posts\/3253\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/media\/1072"}],"wp:attachment":[{"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/media?parent=3253"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/categories?post=3253"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/tags?post=3253"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}