ORDER BY 两列(只有一列有值)
ORDER BY with two columns (only one column has a value)
我创建了一个简单的日历,一方面有时间点,另一方面有时间段。
MySQL table(一个有很多0的块意味着:这里没有实际值,因为它是一个时间点而不是一段时间)
+-------+------------+------------+-------------+-------------+
| title | timepoint1 | timepoint2 | timeperiod1 | timeperiod2 |
+-------+------------+------------+-------------+-------------+
| ABC | 2015-07-12 | 12:30:00 | 0000-00-00 | 0000-00-00 |
| UPS | 2015-07-12 | 15:00:00 | 0000-00-00 | 0000-00-00 |
| DEF | 2015-07-30 | 08:00:00 | 0000-00-00 | 0000-00-00 |
| HIJ | 0000-00-00 | 00:00:00 | 2015-07-16 | 2015-07-31 |
| KLM | 0000-00-00 | 00:00:00 | 2015-08-21 | 2015-08-25 |
+-------+------------+------------+-------------+-------------+
我想要这样的输出
+-------+--------------------------+
| title | when? |
+-------+--------------------------+
| ABC | 2015-07-12 12:30 |
| UPS | 2015-07-12 15:00 |
| HIJ | 2015-07-16 to 2015-07-31 |
| DEF | 2015-07-30 8:00 |
| KLM | 2015-08-21 to 2015-08-25 |
+-------+--------------------------+
如您所见,我想按开始时间(无论是时间点还是时间段)对所有标题进行排序,可能包括 12 点等时间。
但是查询应该怎么写,尤其是ORDER BY部分。我如何添加一个 WHERE 子句,它只选择本月或下个月的会议?
您可以连接这两个字段并按此排序。
ORDER BY CONCAT(`timepoint1`,' ',`timepoint2`) ASC
看到某些字段可能为空,那么您将使用 IF 语句,例如:
ORDER BY CONCAT(IF(`timepoint1`!='0000-00-00',`timepoint1`,`timeperioid1`),' ',`timepoint2`) ASC
SELECT title
, IF(timepoint1 = '0000-00-00'
, CONCAT(timeperiod1, " to ", timeperiod2)
, CONCAT(timepoint1, ' ', timepoint2)
) AS `when?`
FROM the_table
ORDER BY GREATEST(timepoint1, timeperiod1), timepoint2, timeperiod2
;
就是说,为什么不只使用两个 DATETIME 或 TIMESTAMP 字段(一个 "from" 和一个 "to")? "Periods" 会使用 '00:00:00' 时间,或者 "points" 会使 "from" 和 "to" 彼此相等,或者可能会有一个额外的 "type" 某种领域。第一条评论也显示了一个涵盖一天的好方法 "period"。
我创建了一个简单的日历,一方面有时间点,另一方面有时间段。
MySQL table(一个有很多0的块意味着:这里没有实际值,因为它是一个时间点而不是一段时间)
+-------+------------+------------+-------------+-------------+
| title | timepoint1 | timepoint2 | timeperiod1 | timeperiod2 |
+-------+------------+------------+-------------+-------------+
| ABC | 2015-07-12 | 12:30:00 | 0000-00-00 | 0000-00-00 |
| UPS | 2015-07-12 | 15:00:00 | 0000-00-00 | 0000-00-00 |
| DEF | 2015-07-30 | 08:00:00 | 0000-00-00 | 0000-00-00 |
| HIJ | 0000-00-00 | 00:00:00 | 2015-07-16 | 2015-07-31 |
| KLM | 0000-00-00 | 00:00:00 | 2015-08-21 | 2015-08-25 |
+-------+------------+------------+-------------+-------------+
我想要这样的输出
+-------+--------------------------+
| title | when? |
+-------+--------------------------+
| ABC | 2015-07-12 12:30 |
| UPS | 2015-07-12 15:00 |
| HIJ | 2015-07-16 to 2015-07-31 |
| DEF | 2015-07-30 8:00 |
| KLM | 2015-08-21 to 2015-08-25 |
+-------+--------------------------+
如您所见,我想按开始时间(无论是时间点还是时间段)对所有标题进行排序,可能包括 12 点等时间。
但是查询应该怎么写,尤其是ORDER BY部分。我如何添加一个 WHERE 子句,它只选择本月或下个月的会议?
您可以连接这两个字段并按此排序。
ORDER BY CONCAT(`timepoint1`,' ',`timepoint2`) ASC
看到某些字段可能为空,那么您将使用 IF 语句,例如:
ORDER BY CONCAT(IF(`timepoint1`!='0000-00-00',`timepoint1`,`timeperioid1`),' ',`timepoint2`) ASC
SELECT title
, IF(timepoint1 = '0000-00-00'
, CONCAT(timeperiod1, " to ", timeperiod2)
, CONCAT(timepoint1, ' ', timepoint2)
) AS `when?`
FROM the_table
ORDER BY GREATEST(timepoint1, timeperiod1), timepoint2, timeperiod2
;
就是说,为什么不只使用两个 DATETIME 或 TIMESTAMP 字段(一个 "from" 和一个 "to")? "Periods" 会使用 '00:00:00' 时间,或者 "points" 会使 "from" 和 "to" 彼此相等,或者可能会有一个额外的 "type" 某种领域。第一条评论也显示了一个涵盖一天的好方法 "period"。