* ,但仍未解决...... *
我想要合并来自两个文件的数据,但我无法表达排序。我想排序第一个文件的CREATED日期和第二个文件的SHARED_DATE(如果它存在且更大)。
file 1 : NODE
fields: CREATED, CHANGED
file 2 : SHARE_CONTENT
fields: SHARED_DATE , NODE (foreign key)
...
SELECT n.created, s.shared_date,
(CASE WHEN s.shared_date <> NULL
then s.shared_date
else n.created
END)
as lastshare
FROM node as n LEFT JOIN share_content as s on (s.nid=n.nid)
ORDER BY lastshare DESC
另一种可能性..
SELECT n.created, s.shared_date,
FROM node as n LEFT JOIN share_content as s on (s.nid=n.nid)
ORDER BY MAX(n.created, s.shared_date) DESC
除了mySQL似乎不喜欢MAX(a,b)
答案 0 :(得分:0)
SELECT n.created, s.shared_date,
(CASE WHEN (s.shared_date IS NOT NULL) And (s.Shared_Date > n.Created)
then s.shared_date
else n.created
END)
as lastshare
FROM node as n LEFT JOIN share_content as s on (s.nid=n.nid)
ORDER BY lastshare DESC
特别注意IS NOT而不是&lt;&gt;。 NULL在SQL中非常非常特殊。
答案 1 :(得分:0)
SELECT n.created, s.shared_date,
(CASE WHEN s.shared_date IS NOT NULL
THEN
(CASE WHEN s.shared_date > n.created
THEN s.shared_date ELSE n.created END)
ELSE n.created
END)
as lastshare
FROM node as n LEFT JOIN share_content as s on (s.nid=n.nid)
ORDER BY lastshare DESC
现在它应该像你想要的那样工作;)