SELECT and ORDER rows from multiple tables

Posted by hardwarehank, Tue Sep 26 17:09:45 UTC 2006

Interesting problem brought up by Itosu. Basically, he had two tables, called rfs and dfs, and both of them had a column called uname. All he wanted to do was select all of rows from both tables and order them by uname. After some research, this ended up being trivial:


SELECT * FROM rfs UNION ALL SELECT * FROM dfs ORDER BY uname ASC;

The ORDER BY takes effect on the entire union if it comes at the end. This is very handy, and I have a feeling I will be referring back to it someday.

Tags:

Comments

Have your say

A name is required. You may use Markdown in your comments.