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 > Getting data from 2 tables

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-14-10, 04:26
jonniejoejonson jonniejoejonson is offline
Registered User
 
Join Date: Dec 2010
Posts: 4
Getting data from 2 tables

Table A
Table B

Both table A and table B have a field: fileId

I want to select the fileId’s that are in table B, but not if they are also in the following SELECT result set from table A

SELECT fileId FROM a WHERE filetype=‘jpg’;

I hope you understand… I have only just grasped the concepts of joins but I’m at a loss as to how to do this?… Any help would be much appreciated…

Alternatively is there a way to create a new field on the fly so that when I am looping through the second sql result set there is field that says fileId is in both table B and also in the tableA sql result set….

kind regards J
Reply With Quote
  #2 (permalink)  
Old 12-14-10, 04:55
darek82 darek82 is offline
Registered User
 
Join Date: Dec 2010
Posts: 4
Please provide table structure and example data so we have something to work with. I see what you want to do, and in that case JOIN won't do you any good.
Reply With Quote
  #3 (permalink)  
Old 12-14-10, 05:05
jonniejoejonson jonniejoejonson is offline
Registered User
 
Join Date: Dec 2010
Posts: 4
Thnaks Darek: this is as simple as i can make it... (plus it makes it easier for me to understand!)...

Table A:

fileId (primary index) - bignum
fileType - var
fileName - var


Table B:

fileId - bignum
projectId - bignum

fileId+projectId (unique index)

I want to select all the fileIds in A that are not in the following sql result set:
SELECT fileId FROM B WHERE projectId=‘1’;
Reply With Quote
  #4 (permalink)  
Old 12-14-10, 05:42
jonniejoejonson jonniejoejonson is offline
Registered User
 
Join Date: Dec 2010
Posts: 4
cretind tumblers

Thanks to Subdee from another forum for that...

SELECT b.fileId FROM tableB b
WHERE b.fileId NOT IN
(SELECT a.fileId FROM tableA a WHERE filetype='jpg')
AND filetype='jpg'
Reply With Quote
  #5 (permalink)  
Old 12-14-10, 06:08
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by jonniejoejonson View Post
Alternatively is there a way to create a new field on the fly so that when I am looping through the second sql result set there is field that says fileId is in both table B and also in the tableA sql result set….
Code:
SELECT b.fileId 
     , CASE WHEN a.fielId = b.fileId
            THEN 'yes' ELSE 'no' END   AS in_both
  FROM tableB b 
LEFT OUTER
  JOIN tableA a 
    ON a.fileId = b.fileId 
   AND a.filetype = 'jpg'
 WHERE b.filetype = 'jpg'
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 12-14-10, 06:42
jonniejoejonson jonniejoejonson is offline
Registered User
 
Join Date: Dec 2010
Posts: 4
Thanks r937, i will take a look at that... it looks genius!...
kind regards Jonathan
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