Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.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 - Fatal编程技术网

Mysql 通过更改布尔值进行分组

Mysql 通过更改布尔值进行分组,mysql,sql,Mysql,Sql,您好,我有以下SQL问题: SELECT station_id, filling_station_status,date_created , case when filling_station_status="FREE" then 0 else 1 end as status FROM efahrung.electric_station_time_status where station_id=11 在我的表格中,有一列填写站状态。 它可以是免费的,也可以在使用中 我想

您好,我有以下SQL问题:

SELECT station_id, filling_station_status,date_created ,
  case when filling_station_status="FREE" then 0
      else 1 end  as status  
FROM efahrung.electric_station_time_status 
where station_id=11
在我的表格中,有一列填写站状态。 它可以是免费的,也可以在使用中

我想对元素进行分组,这样,如果加油站的状态从“免费”更改为“使用中”,它将创建一个日期范围(在我的示例中为“已创建日期”)。 在下一次从“使用”更改为“释放”时,它会创建一个新的日期范围


谢谢你的建议

如果您只需要SQL查询在输出中生成日期范围,请尝试以下操作:

Select s.station_id, 
    Coalesce(e.filling_station_status, s.filling_station_status) fillingStationStatus, 
    case e.filling_station_status 
      when "FREE" then 0 else 1 end status,
    s.date_created startDate,
    e.date_created endDate
From efahrung.electric_station_time_status s
   Left Join efahrung.electric_station_time_status e
       On e.station_id = s.station_id
          and s.filling_station_status = 'IN_USE'
          and e.filling_station_status = 'FREE'
          and e.date_created =
               (Select Min(date_created)
                From efahrung.electric_station_time_status 
                Where station_id = s.station_id
                   and date_created > s.date_created)

请更具体地说明你的问题是什么。。。是否要向数据库表中添加新列?或者您只是想要一个SQL查询来生成这个新的日期范围作为输出?听起来好像您正在尝试创建一个。提供一些示例数据?现在我有以下观点:[link]我想要这样的东西:[link]你的主键是什么?