我想合并来自两个不同表的结果,他们唯一的共同点就是名为date_mod的日期字段
下面的查询仅显示第一个select语句中的两列。未显示profile_id列。
有关如何调整它的任何帮助,以便我可以根据date_mod字段的顺序将这两个表的最新结果组合起来。
SELECT resume_id, date_mod FROM resumes ORDER BY date_mod
UNION ALL
SELECT profile_id, date_mod FROM profiles ORDER BY date_mod LIMIT 5
答案 0 :(得分:6)
显示了profile_id,但是列只有一个标签 - 第一个选择的标签。
SELECT resume_id as id, date_mod as date, 'resume' as type FROM resumes
UNION ALL
SELECT profile_id, date_mod, 'profile' FROM profiles
ORDER BY date
LIMIT 5
答案 1 :(得分:1)
试试这个:
SELECT *
FROM
(SELECT resume_id as `ID`, date_mod
FROM resumes
UNION
SELECT profile_id as `ID`, date_mod
FROM profiles) iResult
ORDER BY iResult.date_mod
LIMIT 5
答案 2 :(得分:0)
我认为限制5导致您应该包装查询的问题,即
SELECT * FROM (SELECT resume_id,date_mod FROM resumes ORDER BY date_mod UNION ALL SELECT profile_id, date_mod FROM profiles ORDER BY date_mod) LIMIT 5
这应该提供你想要的东西
答案 3 :(得分:0)
您应该在合并基础上申请订单:
(SELECT resume_id, date_mod FROM resumes
UNION ALL
SELECT profile_id, date_mod FROM profiles ) ORDER BY date_mod LIMIT 5
答案 4 :(得分:0)
执行UNION时,列必须具有相同的“别名”列结果和数据类型才能符合条件。如果你想要前五名,你应该实际应用两个限制来从两边获得MOST 5。然后应用OVERALL Limit 5,这样您就不会从两个单独的文件中预先返回所有记录
select
Source,
SrcID,
Date_Mod
from
( SELECT
"R" as Source,
resume_id as SrcID,
date_mod
FROM
resumes
ORDER BY
date_mod
LIMIT 5
UNION ALL
SELECT
"P" as Source,
profile_id as SrcID,
date_mod
FROM
profiles
ORDER BY
date_mod
LIMIT 5 ) PreQuery
order by
Date_Mod
LIMIT 5