views:

128

answers:

4

I'm trying to select rows with a certain column name and order them by another column name.

Here's my problem, into N simplified tables:

table 1: id, username, datetime, comment

table 2: id, username, datetime, vote

.

.

.

table N: id, username, datetime, bought

I want to be able to select all rows where username='Some Name', ordered by datetime -- across all N tables.

--

Incidentally, does anyone know of a good book or resource on learning and practicing advanced SQL? (Tried SQLZoo, but they don't have enough advance examples.. and then there are the too-advanced ones that I'm still stumped on)

+1  A: 

Alias your tables in the query, ie.

SELECT t1.id, t2.username, ... FROM table1 as t1
Janus Tøndering
problem is how do you order everything by datetime?
ina
do you want to sort across all tables?
Janus Tøndering
yup, that's the problem, sorting across all N tables.
ina
think you should be able to order them like this: ORDER BY t1.datetime, t2.datetime, t3.datetime (EDIT: obviously that is not what you want - you will probably have to go with unions as others have suggested).
Janus Tøndering
A: 

You can select same column names from different tables like:

SELECT table1.username as t1user, table2.username as t2user...
fabrik
problem is how do you order *everything* by datetime?
ina
+1  A: 

If you're looking to sort across ALL the tables at the same time (a newer record from table 3 shows up before something from table 2, etc)

The easiest way to do this across multiple tables would be a UNION:

(SELECT id, username, datetime FROM table 1 WHERE username = 'Some Name')
UNION 
(SELECT id, username, datetime FROM table 2 WHERE username = 'Some Name')
UNION
(SELECT id, somethingelse AS username, date as datetime FROM table 3 WHERE somethingelse = 'Some Name')
....
ORDER BY datetime DESC

Note that when you use a union like this, all of your select statements should return the same number of columns and be named the same (you can use aliases for this as I showed above in table 3)

AvatarKava
the other two folks suggest alias as a possibility, too (although i am not sure how they are ordering *everything* by datetime ... if both are possible, is alias vs union better in terms of resources?
ina
If you're looking to order across multiple tables that do not have direct relations, UNIONs are the way to go - that said, this is not especially good for resources since you're making a bunch of subqueries and is usually a sign that you need to change your database schema. Also note that if you want to do something like get the 5 newest records, you'll need to set a LIMIT 5 in EACH of the subqueries and then limit on the outer query as well.
AvatarKava
ok, the solution might be just to create an overall dump_transactions table that concatenates everything
ina
A: 

You can use UNIONS

Select must be somthing like this:

(SELECT comment, date FROM table1 WHERE username='Some Name')
UNION
(SELECT vote, date FROM table2 WHERE username='Some Name')
...
UNION
(SELECT bought, date FROM tableN WHERE username='Some Name')
ORDER BY date LIMIT 100;
Liutas