You cannot use order by statement before concatenation. If you want to use first, you need to convert the result as a temporary table. then say the order as below
select * from (SELECT * FROM comments JOIN users ON comments.user_id = users.id UNION SELECT * FROM comments JOIN videos ON comments.video_id = videos.id) as t order by id DESC LIMIT 1
Itβs important to note that both users and videos must have the same number of columns in the comment table. Because if you use union or union all , you must accept the equal value no. columns with the same data type.
If comments, users, videos have a different number of columns, then the user does not select * from. instead, select all columns as shown below.
If there are user_id, col1, col2, col3 columns in the comment table, then
select user_id,col1,col2,col3 from (SELECT c.user_id,col1,col2,col3 FROM comments c JOIN users u c.user_id = u.id UNION SELECT c.user_id,col1,col2,col3 FROM comments c JOIN videos v ON c.video_id = v.id) as t order by user_id DESC LIMIT 1
Hai
source share