这是我想要做的,但我怀疑它是否可能!
我有两张桌子,一张叫users
,另一张叫answers
。我需要将结果组合成一个语句,并按time
. 棘手的部分是我time
在两个表上都有,我需要考虑这两个time
字段来排序结果。
这是我提出查询的不成功尝试:
SELECT * FROM ( SELECT id, username, first_name, last_name, time FROM users
UNION ALL
SELECT id, answer, pid, uid, time FROM answers ) as AB
ORDER BY `AB.time`
更新
我想出了以下 SQL 查询:
SELECT username AS user, '' as page , '' as content , time FROM users
UNION ALL
SELECT (SELECT username FROM users WHERE `id`=`uid`) AS user, pid AS page, answer AS content, time FROM answers
ORDER BY `time` DESC
它是我正在寻找的,但由于某种原因,user
第二个 SELECT 查询中的字段显示为null
!知道为什么吗?