Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sql Merging two result sets

Tags:

merge

sql

Lets take two result sets as in: 1,2,3,5,7 and 2,4,6,7,8

In the end I want 1,2,3,4,5,6,7,8. I can not figure out how to code this in sql. Can any one give me some suggestions? I've seen some merging functions out there but having trouble implementing something simple.

like image 288
David Avatar asked Mar 13 '26 16:03

David


2 Answers

You may use UNION

(SELECT id FROM table1 WHERE 1=1)
UNION
(SELECT id FROM table2 WHERE 1=1)
ORDER BY id
like image 172
fyr Avatar answered Mar 15 '26 06:03

fyr


I think maybe you're thinking of UNION?

If SELECT `Column` FROM `Table` yields 1,2,3,5,7

And SELECT `Column` FROM `Table2` yields 2,4,6,7,8

Then

SELECT `Column` FROM `Table`
UNION
SELECT `Column` FROM `Table2`

yields 1,2,3,4,5,6,7,8

like image 29
Paul Avatar answered Mar 15 '26 05:03

Paul



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!