我正在寻找根据我使用2个日期(创建和修改)的事实来命令我的mysql数据查询的方法。显然,当创建回来时设置修改。
如果存在或创建,我想订购第一优先权。
显然,我想向那些独立访问或回来的人们展示,或者回来。
QUERY:
SELECT userfrom, idrelation_user_visite_user FROM relation_user_visite_user WHERE userto = 1 AND SUBSTR(created,1,10) < '2013-12-13' ORDER BY created, modified DESC LIMIT 50
(!!!)重要提示:只是理解我必须创建和修改一种数组(理论)分组,然后命令所有DESC
SRC - SQL:
CREATE TABLE IF NOT EXISTS `relation_user_visite_user` (
`idrelation_user_visite_user` int(11) NOT NULL AUTO_INCREMENT,
`created` datetime DEFAULT NULL,
`modified` datetime DEFAULT NULL,
`deleted` datetime DEFAULT NULL,
`userfrom` int(11) DEFAULT NULL,
`userto` int(11) DEFAULT NULL,
`active` int(1) DEFAULT '1',
PRIMARY KEY (`idrelation_user_visite_user`),
KEY `fk_relation_user_visite_user_users1` (`userfrom`),
KEY `fk_relation_user_visite_user_users2` (`userto`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1260 ;
INSERT INTO `relation_user_visite_user` (`idrelation_user_visite_user`, `created`, `modified`, `deleted`, `userfrom`, `userto`, `active`) VALUES
(61, '2013-12-13 15:26:08', NULL, NULL, 292, 1, 1),
(64, '2013-10-21 22:48:04', '2013-11-24 21:10:24', NULL, 292, 1, 1),
(93, '2013-10-23 13:24:30', '2013-11-09 16:06:47', NULL, 292, 1, 1),
(132, '2013-10-24 20:07:39', NULL, NULL, 292, 1, 1),
(202, '2013-10-27 21:40:29', '2013-11-15 00:58:37', NULL, 292, 1, 1),
(311, '2013-11-03 18:28:49', NULL, NULL, 292, 1, 1),
(379, '2013-11-06 13:40:50', NULL, NULL, 292, 1, 1),
(407, '2013-11-06 18:27:50', '2013-11-06 18:48:18', NULL, 292, 1, 1),
(521, '2013-11-09 16:06:36', '2013-12-10 19:16:21', NULL, 292, 1, 1),
(563, '2013-11-11 22:51:46', NULL, NULL, 292, 1, 1),
(722, '2013-11-18 05:08:09', '2013-11-18 05:21:37', NULL, 292, 1, 1),
(765, '2013-11-18 20:11:17', NULL, NULL, 292, 1, 1),
(859, '2013-11-23 01:04:11', '2013-12-10 18:48:50', NULL, 292, 1, 1),
(935, '2013-11-27 16:15:36', '2013-12-02 11:48:24', NULL, 292, 1, 1),
(982, '2013-12-01 18:00:48', NULL, NULL, 292, 1, 1),
(992, '2013-12-02 14:21:46', '2013-12-05 12:25:08', NULL, 292, 1, 1),
(1011, '2013-12-03 09:19:59', '2013-12-03 20:22:11', NULL, 292, 1, 1),
(1019, '2013-12-03 11:09:08', NULL, NULL, 292, 1, 1),
(1026, '2013-12-03 15:47:07', NULL, NULL, 292, 1, 1),
(1053, '2013-12-03 22:36:42', '2013-12-03 22:45:56', NULL, 292, 1, 1),
(1055, '2013-12-03 23:27:10', NULL, NULL, 292, 1, 1),
(1066, '2013-12-04 10:53:18', NULL, NULL, 292, 1, 1),
(1081, '2013-12-04 12:57:26', NULL, NULL, 292, 1, 1),
(1122, '2013-12-04 23:39:14', NULL, NULL, 292, 1, 1),
(1154, '2013-12-05 22:21:23', '2013-12-10 00:01:58', NULL, 292, 1, 1),
(1185, '2013-12-06 22:12:15', NULL, NULL, 292, 1, 1),
(1246, '2013-12-09 19:10:12', '2013-12-09 19:57:12', NULL, 292, 1, 1),
(1259, '2013-12-10 18:55:07', NULL, NULL, 292, 1, 1);
答案 0 :(得分:4)
我假设您不能使用简单ORDER BY modified
,因为有时修改为NULL。如果是这样,请使用
ORDER BY COALESCE(modified, created)
COALESCE函数返回第一个非空值。
答案 1 :(得分:2)
我假设在创建记录时,修改后的列被设置为null,因此您可以使用CASE
SELECT userfrom, idrelation_user_visite_user
FROM relation_user_visite_user,
(CASE WHEN modified IS NOT NULL THEN modified
ELSE created END) custom_order
WHERE userto = 1
AND SUBSTR(created,1,10) < '2013-12-13'
ORDER BY
CASE WHEN modified IS NOT NULL THEN modified
ELSE created END)
LIMIT 50
或者您可以比较列
SELECT userfrom, idrelation_user_visite_user
FROM relation_user_visite_user
WHERE userto = 1
AND SUBSTR(created,1,10) < '2013-12-13'
ORDER BY
(CASE WHEN modified > created THEN modified
ELSE created END) DESC
LIMIT 50
答案 2 :(得分:2)
使用案例:
SELECT userfrom, idrelation_user_visite_user ,
case when modified is not null then modified else created end as last_visit
FROM relation_user_visite_user
WHERE userto = 1
AND SUBSTR(created,1,10) < '2013-12-13'
ORDER BY
case
when modified is not null then modified
else created
end DESC
limit 50
答案 3 :(得分:-2)
SELECT IF( modified IS NOT NULL, modified, created ) as orderdatas,
created, modified, userfrom, idrelation_user_visite_user
FROM relation_user_visite_user
WHERE userto = 1
AND SUBSTR(created,1,10) < '2013-12-13'
ORDER BY orderdatas DESC LIMIT 50