| | |
Question about SELECT statement
Please support our MySQL advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Jun 2005
Posts: 92
Reputation:
Solved Threads: 0
In database I have three tables: t1,t2,t3
all three tables contain the same field (username)
t1(username)
-------
user1
user2
user3
user4
user5
t2(username)
-------
user3
user4
t3(username)
-------
user2
Now how can I write SELECT statment to retrieve all username from table1(t1) which is NOT included in table2(t2) and table3(t3)? thanks.
all three tables contain the same field (username)
t1(username)
-------
user1
user2
user3
user4
user5
t2(username)
-------
user3
user4
t3(username)
-------
user2
Now how can I write SELECT statment to retrieve all username from table1(t1) which is NOT included in table2(t2) and table3(t3)? thanks.
•
•
Join Date: Jun 2005
Posts: 92
Reputation:
Solved Threads: 0
still have problem.
For example:
If table "memberhist" has only one record, then I can get correct result(find users which in table "authuser" but not in "memberhist"), however if I add a new entry to "memberhist", then the result is wrong, see below:
Suppose to get the result of "user12,user2", but it shows "user12,user2,user3,user1".
Don't know how to fix it.
For example:
MySQL Syntax (Toggle Plain Text)
mysql> SELECT uname FROM authuser; +--------+ | uname | +--------+ | user1 | | user12 | | user2 | | user3 | +--------+ 4 rows IN SET (0.00 sec) mysql> SELECT uname FROM memberhist; +-------+ | uname | +-------+ | user1 | +-------+ 1 row IN SET (0.00 sec) mysql> SELECT DISTINCT a.uname FROM authuser a,memberhist b -> WHERE a.uname<>b.uname; +--------+ | uname | +--------+ | user12 | | user2 | | user3 | +--------+ 3 rows IN SET (0.00 sec)
If table "memberhist" has only one record, then I can get correct result(find users which in table "authuser" but not in "memberhist"), however if I add a new entry to "memberhist", then the result is wrong, see below:
MySQL Syntax (Toggle Plain Text)
mysql> SELECT uname FROM authuser; +--------+ | uname | +--------+ | user1 | | user12 | | user2 | | user3 | +--------+ 4 rows IN SET (0.00 sec) mysql> SELECT uname FROM memberhist; +-------+ | uname | +-------+ | user1 | | user3 | +-------+ 2 rows IN SET (0.01 sec) mysql> SELECT DISTINCT a.uname FROM authuser a,memberhist b -> WHERE a.uname<>b.uname; +--------+ | uname | +--------+ | user12 | | user2 | | user3 | | user1 | +--------+ 4 rows IN SET (0.00 sec)
Suppose to get the result of "user12,user2", but it shows "user12,user2,user3,user1".
Don't know how to fix it.
•
•
Join Date: Jun 2005
Posts: 71
Reputation:
Solved Threads: 1
Hello,
The reason that your query does not work correctly is because by using <> in this manner you are creating a 'partial' Cartesian Join between the two tables. In doing so the query returns you a 'partial' Cartesian Product. Though this fact is masked from you in your first example by the use of 'distinct'. Run your query using 'select *' and you will see what is really happening. Google for Cartesian Join/Cartesian Product to find out more.
The example below is in Oracle syntax. So I suspect you will have to translate the (+) syntax for use with MySQL. It is an inner or outer join, I can never remember which. I just know which side of the join to place the (+) when needed
Hope that helps
Kate
The reason that your query does not work correctly is because by using <> in this manner you are creating a 'partial' Cartesian Join between the two tables. In doing so the query returns you a 'partial' Cartesian Product. Though this fact is masked from you in your first example by the use of 'distinct'. Run your query using 'select *' and you will see what is really happening. Google for Cartesian Join/Cartesian Product to find out more.
The example below is in Oracle syntax. So I suspect you will have to translate the (+) syntax for use with MySQL. It is an inner or outer join, I can never remember which. I just know which side of the join to place the (+) when needed

MySQL Syntax (Toggle Plain Text)
Oracle9i Enterprise Edition Release 9.2.0.1.0 - Production With the Partitioning, OLAP AND Oracle Data Mining options JServer Release 9.2.0.1.0 - Production SQL> SET feedback on; SQL> SELECT * FROM users1; USERNAME -------------------------------------------------- user1 user2 user3 user4 user5 user6 6 rows selected. SQL> SELECT * FROM users2; USERNAME -------------------------------------------------- user3 user4 user6 3 rows selected. SQL> SELECT DISTINCT(u1.username) 2 FROM users1 u1, 3 users2 u2 4 WHERE u1.username = u2.username(+) 5 AND u2.username IS NULL; USERNAME -------------------------------------------------- user1 user2 user5 3 rows selected. SQL> DELETE FROM users2 u2 WHERE u2.username = 'user3'; 1 row deleted. SQL> SELECT DISTINCT(u1.username) 2 FROM users1 u1, 3 users2 u2 4 WHERE u1.username = u2.username(+) 5 AND u2.username IS NULL; USERNAME -------------------------------------------------- user1 user2 user3 user5 4 rows selected. SQL> DELETE FROM users2; 2 rows deleted. SQL> SELECT DISTINCT(u1.username) 2 FROM users1 u1, 3 users2 u2 4 WHERE u1.username = u2.username(+) 5 AND u2.username IS NULL; USERNAME -------------------------------------------------- user1 user2 user3 user4 user5 user6 6 rows selected. SQL> ROLLBACK; ROLLBACK complete. SQL> SELECT DISTINCT(u1.username) 2 FROM users1 u1, 3 users2 u2 4 WHERE u1.username = u2.username(+) 5 AND u2.username IS NULL; USERNAME -------------------------------------------------- user1 user2 user5 3 rows selected.
Kate
•
•
Join Date: Jun 2005
Posts: 71
Reputation:
Solved Threads: 1
It is an ugly solution I would not personally use in Oracle, though as you say you cannot use sub-querys, I know of on better method.
Also, in my example you do not need to use the 'distinct' keyword, but it may save you problems down the line should you ever get duplicate records in the table. Though a good index enforcing a unique constraint would stop that altheother
Kate
Also, in my example you do not need to use the 'distinct' keyword, but it may save you problems down the line should you ever get duplicate records in the table. Though a good index enforcing a unique constraint would stop that altheother

Kate
![]() |
Similar Threads
- Question about SELECT statement (MS SQL)
- Hi,Need help on sql select statement structure (MS SQL)
- Question about select statement (MySQL)
- select statemt LIKE (MySQL)
- Getting current value in Select Statement (MS SQL)
- Program Problem with a select statement to access Data base (C)
Other Threads in the MySQL Forum
- Previous Thread: arrange date&time column
- Next Thread: Team of Web developers is looking for customers
Views: 4102 | Replies: 8
| Thread Tools | Search this Thread |
Tag cloud for MySQL
"use" 1 agplv3 alfresco amazon api artisticlicense breathalyzer camparingtocolumns changingprices cmg contentmanagement count court crm data database design developer development drupal dui ec2 eliminate email enter enterprise error eudora facebook form foss gartner gnu government gpl greenit groklaw groupware images innerjoins insert ip joebrockmeier join journalism keywords laptop law legal license licensing linux maintenance managing matchingcolumns metron micromanage microsoft microsoftexchange mindtouch montywidenius mozilla multiple music mysql mysqlcolumnupdating mysqldatetimeordermax() mysqlindex mysqlinternalqueries mysqlquery mysqlsearch news open-xchange opendatabasealliance opengovernment opensource operand oracle pdf penelope php priceupdating referencedesign remove reorderingcolumns resultset saas select sharepoint sourcecode spotify sql sugarcrm syntax techsupport thunderbird transparency update virtualization





