Mysql union different number of columns

I know that join queries must have the same number of columns. I am trying to get results from a table commentsand results from a table stringsthat has multiple joins. How is that right? I have not tested yet, because I know that I am getting an error with a different number of columns. Here are two queries I'm trying to combine.

query 1 (rows)

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 

request 2 (comments)

SELECT * FROM comments WHERE viewed='0' AND (`starter-id`=:userid OR respondid=:userid)

I would like to order the results on the timestamp column posted(most recent)ORDER BY posted DESC

How to combine these queries?

+5
source share
3 answers

You want to select columns as NULLto tackle white space in specific tables.

Table A: (id, column1)

B: (id, column1, column2)

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

(null as columnName) . *, .

+1

CONCAT_WS, , .

SELECT `r_id`, `r_added`, CONCAT_WS('###',`otherfield1`,`otherfield2`) as r_other FROM table1
UNION
SELECT `r_id`, `r_added`, CONCAT_WS('###',`otherfield3`,`otherfield4`,`otherfield5`) as r_other FROM table2

, .

$rother = explode("###", $row['r_other']);

, - . , . # t1 # # t2 # , .

+1

All Articles