简体   繁体   中英

Why isn't this easy join working? (mysql)

I have two tables, classified and fordon .

classified table:
classified_id (PK)
etc...

fordon table:
id (PK)
classified_id (FK)

I try to use this code:

SELECT * FROM classified, fordon WHERE classified.ad_id IN ('$solr_id_arr_imploded') AND classified.classified_id=fordon.classified_id

BTW, the array is a set of ad_id:s returned from solr, never mind that, that is not the problem here...

Then I use mysql_fetch_array in a while-loop to display all the results:

while($row = mysql_fetch_array($qry_result)){

but when I try to echo something which is inside the table fordon , then the index can't be found error appears. But whatever is inside the table classified works to echo!

Any ideas?

Thanks

UPDATE

   while($row = mysql_fetch_array($qry_result)){
   echo $row['type']; // This doesn't work, because the 'type' column is inside the 'fordon' table
   echo $row['headline']; // This does work because it's inside 'classified' table.

Does this help?

SELECT * 
FROM classified c
INNER JOIN fordon f ON c.classified_id=f.classified_id
WHERE classified.ad_id IN ('$solr_id_arr_imploded');

Also, its generally not a good idea to use: SELECT * . Its better to either select only the elements you want or use the * in context of the table you are getting all from, eg

SELECT classified.* 
FROM classified c
INNER JOIN fordon f ON c.classified_id=f..classified_id
WHERE classified.ad_id IN ('$solr_id_arr_imploded');

When you do joins with a blanket * you get every field in all tables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM