Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 列中项目之间的差异_Mysql_Sql_Select - Fatal编程技术网

Mysql 列中项目之间的差异

Mysql 列中项目之间的差异,mysql,sql,select,Mysql,Sql,Select,我将数据存储在打开和关闭时间位于不同行的位置。数据的组织方式如下: id type mon tues wed ... 1 OPEN 09:30:00 09:30:00 10:00:00 1 CLOSE 22:00:00 22:00:00 21:00:00 2 OPEN 09:00:00 09:00:00 09:00:00 2 CLOSE 22:00:00 22:00:00 21:00:00

我将数据存储在打开和关闭时间位于不同行的位置。数据的组织方式如下:

id   type   mon       tues       wed       ...
1    OPEN   09:30:00  09:30:00   10:00:00
1    CLOSE  22:00:00  22:00:00   21:00:00
2    OPEN   09:00:00  09:00:00   09:00:00 
2    CLOSE  22:00:00  22:00:00   21:00:00 
id   mon       tues       wed ...
1    12.5      12.5       11 
2    13        13         12
对数据进行组织,以便打开和关闭时间(本地时间)位于不同的行上

我正在尝试创建一个select查询,该查询将输出如下数据:

id   type   mon       tues       wed       ...
1    OPEN   09:30:00  09:30:00   10:00:00
1    CLOSE  22:00:00  22:00:00   21:00:00
2    OPEN   09:00:00  09:00:00   09:00:00 
2    CLOSE  22:00:00  22:00:00   21:00:00 
id   mon       tues       wed ...
1    12.5      12.5       11 
2    13        13         12

其中,输出值=
时间到秒(TIMEDIFF(关闭时间、打开时间))/3600.0
。换句话说,商店的营业时间。开盘时间和收盘时间在不同的一排,这一事实真的让我很反感。谢谢

一种方法使用
timestampdiff()


一种方法使用
timestampdiff()


这个问题可以通过自连接来解决

select * from store s1 join store 
 s2 on s1.storeId = s2.storeId ;
因此,自联接之后,结果集如下所示

id   s1.type   s1.type      s1.mon   s2.mon          s1.tues    s2.tues         
 1    OPEN     CLOSE       09:30:00   09:30:00        09:30:00  10:00:00

正如您在自联接后看到的,打开和关闭行位于同一行中,因此您可以应用您的函数来计算时间

这个问题可以通过自联接来解决

select * from store s1 join store 
 s2 on s1.storeId = s2.storeId ;
因此,自联接之后,结果集如下所示

id   s1.type   s1.type      s1.mon   s2.mon          s1.tues    s2.tues         
 1    OPEN     CLOSE       09:30:00   09:30:00        09:30:00  10:00:00

正如您在自我连接后看到的,在同一行中打开和关闭行,因此您可以应用您的函数来计算时间

当我使用此查询时,我为timestampdiff()的输出获取空值。当我使用此查询时,我为timestampdiff()的输出获取空值。