If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > "A different B" How to SELECT data in the SQL language.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-22-03, 05:26
kego kego is offline
Registered User
 
Join Date: Oct 2003
Posts: 10
"A different B" How to SELECT data in the SQL language.

I have 2 tables.

"TABLE_A" has "TA_ID" field
The value in this field is 1 , 2 , 3 , 4 , 5 , 6 and 7

and

"TABLE_B" has "TB_ID" field
The value in this field is 1 , 2 , 3 and 8

TABLE_A.TA_ID different TABLE_B.TB_ID is 4 , 5 and 6 (follow to mathematics)

How to? i want to select that data in the SQL language by use SELECT command.

Thanks
Reply With Quote
  #2 (permalink)  
Old 10-22-03, 05:45
Lazy Lazy is offline
Registered User
 
Join Date: Sep 2003
Location: Brussel
Posts: 52
Re: "A different B" How to SELECT data in the SQL language.

So you like to select the data that is in table A but not in table B. Right?

SELECT DISTINCT ta_id
FROM table_a
WHERE ta_id NOT IN
(SELECT tb_id
FROM table_b);


It is possible that you can do it easier, but this works to.
__________________
A good programmer is a LAZY programmer!
Reply With Quote
  #3 (permalink)  
Old 10-22-03, 06:36
Mincer Mincer is offline
Registered User
 
Join Date: Sep 2003
Location: London
Posts: 56
Re: "A different B" How to SELECT data in the SQL language.

Quote:
Originally posted by Lazy
So you like to select the data that is in table A but not in table B. Right?

SELECT DISTINCT ta_id
FROM table_a
WHERE ta_id NOT IN
(SELECT tb_id
FROM table_b);


It is possible that you can do it easier, but this works to.
Note that MySQL does not support subqueries, so unless you are using the latest alpha release, this will not work.
Reply With Quote
  #4 (permalink)  
Old 10-24-03, 11:58
asherh asherh is offline
Registered User
 
Join Date: Jul 2003
Posts: 34
Hi,

You could try the following without subselects...

SELECT ta.id from table_a AS ta LEFT JOIN
table_b AS tb ON ta.id = tb.id
WHERE tb.id IS NULL;

This will display something like...

+------+
| id |
+------+
| 4 |
| 5 |
| 6 |
| 7 |
+------+
4 rows in set (0.00 sec)

cheers,
ash
Reply With Quote
  #5 (permalink)  
Old 10-27-03, 03:17
kego kego is offline
Registered User
 
Join Date: Oct 2003
Posts: 10
Thank you asherh

Your script is correct. i tried that script.

If the TABLE_B have small records. This script is work.
But, if the TABLE_B have many records.
The script will be run long time so much.
and display "Fatal error: Maximum execution time of 30 seconds exceeded in"
it's over time.
Reply With Quote
  #6 (permalink)  
Old 10-27-03, 14:53
Mincer Mincer is offline
Registered User
 
Join Date: Sep 2003
Location: London
Posts: 56
Are there indexes on the 2 id columns?
Reply With Quote
  #7 (permalink)  
Old 10-27-03, 22:56
kego kego is offline
Registered User
 
Join Date: Oct 2003
Posts: 10
No, The 2 id column are not primary and index.

What is a problem?
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On