472,353 Members | 2,059 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

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 12441
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Marco Alting | last post by:
Hi I have two table which are related: table1 holds personellinformation table2 holds nodeInformation The nodes in table2 can have a...
0
by: BobG | last post by:
-- The issue This following query works fine on Toad and PowerBuilder yet when it is run on SQLPlus I get: "ORA-03113: end-of-file on communication...
14
by: Craig Hoskin | last post by:
Hi everyone Have a problem I would areally appreciate help with. I have 3 tables in a standard format for a Bookshop, eg Products Categories...
5
by: Reestit Mutton | last post by:
Hi, I'm currently learning the ropes with CSS, PHP, MYSQL, Javascript etc... by redesigning my website as a database driven site. Okay, so I'm...
1
by: anmar | last post by:
I'm trying to figure out how to select all the records in one table which have multiple specified records in a second table. Here's a simplified...
2
by: Michael Howes | last post by:
I have a single DataTable in a DataSet. It has 4 columns and i'd like to get a handful of counts of unique items in 3 of the 4 columns. Can a...
4
by: Ed L. | last post by:
I think I'm seeing table-level lock contention in the following function when I have many different concurrent callers, each with mutually distinct...
1
by: zafm86 | last post by:
Hi everyone! I'm sure my problem is pretty easy to solve but I've been working on it for a long and my my brain is not working correctly anymore....
5
by: CindySue | last post by:
Hello, I'm so stuck and this seems like it should be so simple. I have three tables, called Auction Items, Party #1 and Party #2. A bidder...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.