mysql question

Discussion in 'Databases' started by dare2k, Jan 12, 2010.

  1. #1
    hi,

    I have a problem.
    I have 2 tables (shopping_cart,customer) that both have buyer_id.
    
    $query = "SELECT buyer_id FROM shopping_cart,customer WHERE shopping_cart.buyer_id=customer.buyer_id;"; 
    $result = mysql_query($query) or die(mysql_error());
    
    Code (markup):
    I get this error message:
    Column 'buyer_id' in field list is ambiguous

    Can you help me?
    I need to insert the buyer_id s to another table.
     
    dare2k, Jan 12, 2010 IP
  2. rayqsl

    rayqsl Active Member

    Messages:
    91
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    53
    #2
    You need to qualify the buyer_id that you are selecting:-

    SELECT shopping_cart.buyer_id FROM shopping_cart,customer WHERE shopping_cart.buyer_id=customer.buyer_id

    Most people use aliases for the tables because it reduces the amount of keying of qualifications. So you could do the following:-

    SELECT s.buyer_id FROM shopping_cart s,customer c WHERE s.buyer_id=c.buyer_id
     
    rayqsl, Jan 12, 2010 IP
  3. rayqsl

    rayqsl Active Member

    Messages:
    91
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    53
    #3
    BTW. You need to qualify the buyer_id because SQL doesn't know which buyer_id you want in your select. Because you're joining the two tables by buyer_id, you can choose from either table.
     
    rayqsl, Jan 12, 2010 IP
  4. dare2k

    dare2k Peon

    Messages:
    80
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks for the help. It works now!
     
    dare2k, Jan 13, 2010 IP