Php 按日期对行中的列进行排序
我有一个表,表中有激活的_at和is_paying列,我在其中存储日期时间,如2015-03-17 16:06:12,is_paying列是一个布尔列,默认为1或0,它将为0,现在我想按照is_paying和激活的_日期范围对项目进行排序。例如,我的表中有如下记录Php 按日期对行中的列进行排序,php,mysql,Php,Mysql,我有一个表,表中有激活的_at和is_paying列,我在其中存储日期时间,如2015-03-17 16:06:12,is_paying列是一个布尔列,默认为1或0,它将为0,现在我想按照is_paying和激活的_日期范围对项目进行排序。例如,我的表中有如下记录 item_id is_paying activated_at 14186 1 2015-03-17 23:40:45 13647 1 2015-03-17
item_id is_paying activated_at
14186 1 2015-03-17 23:40:45
13647 1 2015-03-17 12:43:52
4404 0 2015-03-16 11:58:15
14313 1 2015-03-16 10:42:24
3806 0 2015-03-15 12:37:36
10716 1 2015-03-15 11:01:32
现在我希望输出像
14186 1 2015-03-17 23:40:45
13647 1 2015-03-17 12:43:52
14313 1 2015-03-16 10:42:24
4404 0 2015-03-16 11:58:15
10716 1 2015-03-15 11:01:32
3806 0 2015-03-15 12:37:36
它应该优先在日期激活,然后在它应该排序的范围内,如果它设置为1,是否支付给顶部,有人能帮我吗
到目前为止,我有这个
select items.id, CASE WHEN (DATE(items.activated_at) = DATE(now())) THEN items.is_paying END as sort_order from group by items.id order by sort_order desc, items.activated_at desc
试试这个
从表顺序中选择*在DESC处激活,是否支付DESC
按以下顺序执行选择
查询
:
select * from mytable order by date(activated_at) desc, is_paying desc
从给定的预期结果来看,您希望先按日期对数据进行排序,然后按降序排序。你可以随心所欲
select * from table_name order by date(activated_at) desc,is_playing desc;
下面是测试用例
mysql> create table test (item_id int,is_playing int , activated_at datetime);
Query OK, 0 rows affected (0.09 sec)
mysql> insert into test values (14186,1,'2015-03-17 23:40:45'),(13647,1,'2015-03-17 12:43:52'),(4404,0,'2015-03-16 11:58:15'),(14313,1,'2015-03-16 10:42:24'),(3806,0,'2015-03-15 12:37:36'),(10716,1,'2015-03-15 11:01:32');
mysql> select * from test order by date(activated_at) desc,is_playing desc;
+---------+------------+---------------------+
| item_id | is_playing | activated_at |
+---------+------------+---------------------+
| 14186 | 1 | 2015-03-17 23:40:45 |
| 13647 | 1 | 2015-03-17 12:43:52 |
| 14313 | 1 | 2015-03-16 10:42:24 |
| 4404 | 0 | 2015-03-16 11:58:15 |
| 10716 | 1 | 2015-03-15 11:01:32 |
| 3806 | 0 | 2015-03-15 12:37:36 |
+---------+------------+---------------------+
6 rows in set (0.00 sec)
在mysql查询中的
ORDER BY
子句中使用多列。他不想在处激活desc吗?与上面的注释相同,它忽略了is_paying您得到了什么输出?实际上这是我的sql语句select items.activated_at,dealers.is_paying from items内部加入dealers.id=items.dealer_id ORDER BY items.activated_at desc,dealers.is_paying desc根据您的数据,is_paying上的排序将不起作用,因为所有日期都不同。也就是说,它将首先按日期排序(这似乎是您想要的)下一个是你付款,但每个日期只有一个记录,没有进一步的分类,我得到2015-03-17 23:40:45 1 2015-03-17 16:06:12 0 2015-03-17 12:43:52 1 2015-03-16 16:00:33 1 2015-02-02 13:54:09 0 2015-02-02 11:58:31 0 2015-02-01 10:42:24 0 2015-01-31 14:47:34 2015-01-01-29 17:39:34 1所有日期都不尽可能不同请看,我有两个相同的日期,一个设置为0,另一个设置为1