MySQL的联合不同的列数

编程入门 行业动态 更新时间:2024-10-17 05:36:56
本文介绍了MySQL的联合不同的列数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我知道联合查询必须具有相同数量的列.我正在尝试从表comments中获取结果,并从表strings中获取结果,该表具有多个联接.我该如何正确?我尚未进行测试,因为我知道使用不同数量的列会遇到错误.这是我要合并的两个查询.

I know union queries have to have the same number of columns. I'm trying to get results from the table comments and results from the table strings, which has multiple joins. How do i this correctly? I haven't tested yet because i know i'll get an error with different number of columns. Here are the two queries that i'm trying to combine.

查询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

I'd like to order the results by the timestamp column posted (most recent) ORDER BY posted DESC

如何合并这些查询?

推荐答案

您可能希望选择列作为NULL来占用某些表中的空白空间.

You would want to select columns as NULL to take up for the empty space in certain tables.

表A:(id,第1列)

Table A: (id, column1)

表B:(id,column1,column2)

Table B: (id, column1, column2)

Select id, column1, null as column2 from tableA UNION Select id, column1, column2 from tableB

更多推荐

MySQL的联合不同的列数

本文发布于:2023-11-30 09:23:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649491.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:MySQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!