user188962
user188962

Reputation:

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.

Upvotes: 0

Views: 118

Answers (1)

mardala
mardala

Reputation: 237

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, e.g.

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.

Upvotes: 1

Related Questions