0

Possible Duplicate:
How can an SQL query return data from multiple tables

i want a good intel about how to do such a thing : select everything from my table 1 and my table 2 where the id_article of my table 2 is equal to id from my table 1...I try this :

SELECT * FROM table1 AS n, table2 AS a WHERE n.id = a.id_article

But it didn't work...I don't understand the whole process though....

Thanks for any clue

Community
  • 1
  • 1
  • 1
    [How can an SQL query return data from multiple tables](http://stackoverflow.com/questions/12475850/how-can-an-sql-query-return-data-from-multiple-tables). – DCoder Dec 23 '12 at 09:50
  • 1
    your query is absolutely fine although you are using the old syntax of join. how come it is not working? – John Woo Dec 23 '12 at 09:53
  • maybe my way to catch results is not good because i have no error. i see that. But thanks for the tips :) – Aurélien Clochet Dec 23 '12 at 10:06
  • The problem is probably in your PHP, since the SQL is fine. – Barmar Dec 23 '12 at 10:18

2 Answers2

2

Use the JOIN statement.

SELECT * 
  FROM table1 
  JOIN table2 
    ON table1.id = table2.id_article

Optionally you can add WHERE conditions in the end

raina77ow
  • 103,633
  • 15
  • 192
  • 229
Horen
  • 11,184
  • 11
  • 71
  • 113
0

Alternatively, use the new syntax format of the query, ANSI SQL-92.

SELECT  a.*, b.*
FROM    table1 a
        INNER JOIN table2 b
            ON a.ID = b.ID_Article

To fully understand about joins, see the article below

John Woo
  • 258,903
  • 69
  • 498
  • 492
  • Disregarding the fact that the join syntax is preferable, I don't see what's wrong with his query, do you? Just wondering if this will work any better if his didn't work to begin with, or maybe there is a mistake in his query that I just don't see... – Brandon Moore Dec 23 '12 at 09:54
  • @BrandonMoore as stated in my comment above, the query is fine. maybe the OP want *OUTER JOINs*, not sure. But the question is too incomplete. – John Woo Dec 23 '12 at 09:55
  • 1
    Cool, didn't see your comment up there and was just wondering if I was missing something obvious :) – Brandon Moore Dec 23 '12 at 09:56
  • Well i don't have any results, maybe my calls are not good because i have no error messages. – Aurélien Clochet Dec 23 '12 at 10:00
  • the best way you do is to add sample records and desired result along your question `:D` – John Woo Dec 23 '12 at 10:01
  • here my syntax : $req00=mysql_query("SELECT * FROM articles JOIN images ON articles.id = images.id_article"); while ($res00=mysql_fetch_array($req00)){ echo $res00['chemin']; } Nothing happens with my echo...Where 'chemin' is a row of my images table – Aurélien Clochet Dec 23 '12 at 10:09
  • are you certainly sure that there are `ID` on table `articles` that matches on `ID_Articles` on table `images`? – John Woo Dec 23 '12 at 10:11
  • execute the query in phpmyadmin and see if the query works first. then we can fix php problems after :) – Horen Dec 23 '12 at 10:11
  • 1
    lol, yes it is, i have no registred item in my second table ^^. The whole thing works better with something in it :) Thanks to everyone ! – Aurélien Clochet Dec 23 '12 at 11:12