1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Query with conditions from 2 tables

Discussion in 'MySQL' started by Darkhodge, Feb 26, 2010.

  1. #1
    Hey everyone,


    I have 2 tables:

    • phones - This table stores basic information about an individual handset (make, model, imei etc...).
    • additionalInfo - This table stores further information like shape.

    Example tables:

    phones
    
    id   make     model        imei
    1    Nokia    3310         12345
    2    Apple    iPhone 3GS   54321
    3    Apple    iPhone 3GS   33333
    
    Code (markup):
    additionalInfo
    
    id      listingID   shape
    7       1           Block
    8       2           Block
    9       3           Block
    
    Code (markup):
    How would I go about making a query that searches for records from the phones table where it matches the make and model, and then ensures the match also has the right form of shape?

    Just to clarify, what I require IF it were a single table like this:

    Hypothetical Merged Table
    
    id   make     model         imei     shape
    1    Nokia    3310          12345    Block
    2    Apple    iPhone 3GS    54321    Block
    3    Apple    iPhone 3GS    33333    Block
    
    Code (markup):
    What I would need is:
    
    SELECT * FROM `thisTable` WHERE `make` = '$make' AND `model` = '$model' AND `shape` = '$shape'
    
    Code (markup):
    Thanks in advance!
     
    Darkhodge, Feb 26, 2010 IP
  2. jimmy4feb

    jimmy4feb Peon

    Messages:
    56
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hello,

    Following is the answer of your question:

    If you want to select all records

    SELECT * FROM phone, additionalInfo WHERE phone.id = additionalInfo.listingID;

    If you want to select a particular record

    SELECT * FROM phone, additionalInfo WHERE phone.id = 1 AND additionalInfo.listingID = 1;

    Thanks,

    Jimmy
     
    jimmy4feb, Feb 26, 2010 IP
    Darkhodge likes this.
  3. Darkhodge

    Darkhodge Well-Known Member

    Messages:
    2,111
    Likes Received:
    76
    Best Answers:
    1
    Trophy Points:
    185
    #3
    Ahhh that's the one! I learnt it ages ago when I first learned MySQL and I haven't used it since then so I forgot!

    Anyway thanks a lot, very helpful. Rep added! :)
     
    Darkhodge, Feb 27, 2010 IP