SELECT old.REF_ID, old.DATE FROM REFID_HISTORY old
INNER JOIN REFID_HISTORY new
ON new.REF_ID = old.REF_ID
UNION
SELECT new.REF_ID, new.DATE FROM REFID_HISTORY new
INNER JOIN REFID_HISTORY old
ON new.REF_ID = old._REF_ID
ORDER BY DATE DESC;
上面的脚本显示了REFID
的家谱。 REF_ID
如何在不选择的情况下对DATE
进行排序?
答案 0 :(得分:2)
将查询用作子查询,然后仅选择要显示的列。像这样:
SELECT REF_ID
FROM (
SELECT old.REF_ID, old.DATE FROM REFID_HISTORY old
INNER JOIN REFID_HISTORY new
ON new.REF_ID = old.REF_ID
UNION
SELECT new.REF_ID, new.DATE FROM REFID_HISTORY new
INNER JOIN REFID_HISTORY old
ON new.REF_ID = old._REF_ID
) t
ORDER BY DATE DESC;
我创建了一个示例演示看看:
<强>编辑:强> 问。对于Cust_ID ='ABC123'
SELECT REF_ID
FROM (
SELECT old.REF_ID, old.CUST_ID,old.DATE FROM REFID_HISTORY old
INNER JOIN REFID_HISTORY new
ON new.REF_ID = old.REF_ID
UNION
SELECT new.REF_ID, new.CUST_ID, new.DATE FROM REFID_HISTORY new
INNER JOIN REFID_HISTORY old
ON new.REF_ID = old._REF_ID
) t
WHERE CUST_ID = 'ABC123'
ORDER BY DATE DESC;
答案 1 :(得分:1)
我没有得到查询。为什么不这样做?
SELECT h.REF_ID
FROM REFID_HISTORY h
ORDER BY h.DATE DESC;
这似乎比你的方法简单得多。