SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: select join

  1. #1
    SitePoint Guru
    Join Date
    Apr 2001
    Location
    BC, Canada
    Posts
    630
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    select join

    Hi, im trying to get a SELECT join statement workin. How the tables are organized are listed below

    table articles

    |--aid--|--article_headline--|--article_body--|--article_date-------|
    |---1---|---whatever-------|--fdsafdsaf-----|--456784561-----|

    Table city_lookup
    |--aid--|--cid--|
    |---1---|--1---|
    |--1----|--3---|

    Table cities
    |--cid--|--city----|
    |--1----|-Kelowna-|
    |--2----|-vernon--|
    |--3----|-seattle--|

    On one page I am givin the aid in the variable $aid. With this I have to find out everything about this article, as well as what cities it applies to.
    So I came up with this:

    $result= mysql_query("SELECT * FROM articles, cities WHERE aid = '$aid' AND articles.aid=city_lookup.aid AND city_lookup.cid=cities.cid") //get all info in articles table
    or die("Error");

    But its not working, Im getting the error part
    Hope soneone can help

  2. #2
    SitePoint Zealot oodie's Avatar
    Join Date
    Jul 2000
    Location
    Misty Mountain
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First off, you have to include table city_lookup in the query. Second, the clause "aid='$aid'" is ambiguous since you didn't mention the table name. Try thi:
    Code:
    SELECT * FROM articles, cities, city_lookup WHERE
    articles.aid = '$aid' AND 
    articles.aid = city_lookup.aid AND 
    city_lookup.cid = cities.cid


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •