need a SQL command plzzzz

Discussion in 'PHP' started by dracula51, Dec 12, 2009.

  1. #1
    well, first plz see my table's structure

    [​IMG]

    What I want:
    Select all from table 1
    get code of the id from table2 that is declared first in data_ids of table1

    i mean when it select all from table1 & printing them one by one (using while loop) it will also print code (from table2) of first declared id.

    so result will be like this:

    [​IMG]

    i think u understand now. look at the table1. in data_ids field there r some ids & first one is 2
    thats why it take code (=78451) from table2 where id=2


    i know its a confussing post...very messy :( sorry for it
     
    Last edited: Dec 12, 2009
    dracula51, Dec 12, 2009 IP
  2. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #2
    try following..

    SELECT table1.id, title, code FROM table1 JOIN table2 ON LEFT(data_ids, 1) = table2.id;

    If you want to see list from table1 even if there is no entry in table2 for the same, use LEFT JOIN.
     
    mastermunj, Dec 12, 2009 IP
  3. dracula51

    dracula51 Peon

    Messages:
    146
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    tnx :)
    but i fount using TRIM() is better than LEFT() as u dont know the length of the id
    thank you again.
     
    dracula51, Dec 12, 2009 IP