how to write the sql for php?

Discussion in 'PHP' started by myinnet, Mar 10, 2007.

  1. #1
    i try to explain this in proper way, i have two tables in mysql, like:
    table 1:
    id1
    topic
    ........

    table 2:
    id2
    id1 (this id is the id in table1)
    ....

    Now I need get "TOPIC" thru "ID2", Can anyone give me the sql?
    thanks
     
    myinnet, Mar 10, 2007 IP
  2. MakeThatDollar

    MakeThatDollar Notable Member

    Messages:
    4,451
    Likes Received:
    158
    Best Answers:
    0
    Trophy Points:
    225
    #2
    I believe this would be correct:

    mysql_query("SELECT topic FROM table1 WHERE table1.id1=table2.id2");
     
    MakeThatDollar, Mar 10, 2007 IP
  3. Kalyse

    Kalyse Peon

    Messages:
    1,221
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    0
    #3
    select `topic` from `table1` where `table1`.`id1` = `table2`.`id2`
     
    Kalyse, Mar 10, 2007 IP
  4. myinnet

    myinnet Peon

    Messages:
    81
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    i run the sql in mysqladmin, it's said
    #1109 - Unknown table 'table2 in field list
     
    myinnet, Mar 10, 2007 IP
  5. srobona

    srobona Active Member

    Messages:
    577
    Likes Received:
    57
    Best Answers:
    0
    Trophy Points:
    88
    #5
    Please check the spelling of the table2/table 2. :)
     
    srobona, Mar 10, 2007 IP
  6. myinnet

    myinnet Peon

    Messages:
    81
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    No, the table names are right. i just need make sure the sql is right.
     
    myinnet, Mar 10, 2007 IP
  7. Kalyse

    Kalyse Peon

    Messages:
    1,221
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Which query, mine or the other guys?s
     
    Kalyse, Mar 10, 2007 IP
  8. srobona

    srobona Active Member

    Messages:
    577
    Likes Received:
    57
    Best Answers:
    0
    Trophy Points:
    88
    #8
    try this:

    (SELECT topic FROM table1 WHERE table1.id1=table2.id1)

    OR,

    this:

    select table1.topic from table1 where table1.id1 = table2.id1
     
    srobona, Mar 10, 2007 IP
  9. Master_Moving

    Master_Moving Guest

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    i dont think its easy to get understand that without to learn the basic and i would suggest u to go www.php.net and its not a big section of Mysql -> PHP try it. would be the best for u , there are more many options u may want know.
     
    Master_Moving, Mar 11, 2007 IP
  10. Robert Plank

    Robert Plank Peon

    Messages:
    55
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #10
    You are getting the unknown table2 error because you aren't using table2 in the query...

    select table1.topic from table1, table2 where table1.id1 = table2.id1
     
    Robert Plank, Mar 11, 2007 IP
  11. myinnet

    myinnet Peon

    Messages:
    81
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Tobert Plank is right, i just changed the sql, it's return the right result., thank you so much.
     
    myinnet, Mar 11, 2007 IP