469,358 Members | 1,630 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,358 developers. It's quick & easy.

select distinct on two tables

I have two tables, both containing an 'authors' column. Is there a way
to get a unique list of authors from the two tables?
I tried SELECT DISTINCT `authors` from `table1`, `table2`;
but I got an "Column 'authors' in field list is ambiguous" error.

Is there also a query to return only the count of distinct authors from
the two tables?

Thanks for any help.

Jul 23 '05 #1
2 12280
mf*****@gmail.com wrote:
I have two tables, both containing an 'authors' column. Is there a way
to get a unique list of authors from the two tables?
I tried SELECT DISTINCT `authors` from `table1`, `table2`;
but I got an "Column 'authors' in field list is ambiguous" error.
Try this:
(SELECT `authors` FROM `table1`)
UNION
(SELECT `authors` FROM `table2`)
ORDER BY `authors`

See http://dev.mysql.com/doc/mysql/en/union.html for more information.
Is there also a query to return only the count of distinct authors from
the two tables?


For aggregates over multiple tables, I think you'd have to start using
MERGE tables, but this can be used only if the tables have identical
columns.
http://dev.mysql.com/doc/mysql/en/me...ge-engine.html

Another method would be to use a TEMPORARY table:
CREATE TEMPORARY TABLE authorlist (`authors` VARCHAR(100));
INSERT INTO authorlist SELECT DISTINCT `authors` FROM `table1`;
INSERT INTO authorlist SELECT DISTINCT `authors` FROM `table2`;
SELECT DISTINCT `authors` FROM `authorlist`;
SELECT COUNT(DISTINCT `authors`) FROM `authorlist`;

Regards,
Bill K.
Jul 23 '05 #2
Thanks very much !

Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Marco Alting | last post: by
14 posts views Thread by Craig Hoskin | last post: by
5 posts views Thread by Reestit Mutton | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.