By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,300 Members | 1,795 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,300 IT Pros & Developers. It's quick & easy.

select distinct on two tables

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
Thanks very much !

Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.