我知道联合查询必须具有相同数量的列。我正在尝试从表中获取结果comments
以及表中的结果strings
,它有多个连接。我如何正确地做到这一点?我还没有测试过,因为我知道我会收到不同数量的列的错误。这是我试图合并的两个查询。
查询1(字符串)
SELECT sub.actionid as usersub,
ftable.`last-time` as lastvisited, updatetable.recent as mostrecent, parent.* FROM `strings` as parent
LEFT JOIN subscribe sub on sub.actionid=parent.id AND sub.userid=:userid
JOIN followers ftable on ((ftable.sid=parent.sid AND ftable.page='1') OR
(ftable.sid=parent.sid AND ftable.position=parent.position AND ftable.page='0')
OR (ftable.profile=parent.starter AND parent.guideline='1')) AND ftable.userid=:userid
JOIN `update-recent` updatetable on
((updatetable.sid=parent.sid AND updatetable.position=parent.position AND updatetable.pageid='0')
OR (updatetable.profile=parent.starter) OR (updatetable.pageid=parent.pageid AND parent.pageid!=0))
WHERE ftable.userid=:userid AND parent.deleted='0' GROUP BY parent.id
查询2(评论)
SELECT * FROM comments WHERE viewed='0' AND (`starter-id`=:userid OR respondid=:userid)
我想按时间戳列对结果进行排序posted
(最新)ORDER BY posted DESC
我如何合并这些查询?
您希望将列选择为NULL
占用某些表中的空白空间。
表 A:(id,列 1)
表 B:(id、列 1、列 2)
Select id, column1, null as column2 from tableA
UNION
Select id, column1, column2 from tableB
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)