Forum Moderators: coopster

Message Too Old, No Replies

Joining tables

         

derek mcgilvray

6:58 am on Apr 21, 2007 (gmt 0)

10+ Year Member



Hi folks,

Could someone tell me what's wrong with this query? I'm trying to join 3 tables and I've looked into everything to no avail.

$query = "SELECT products_to_categories.*, products_description.*, products.*
FROM products_to_categories
LEFT JOIN products_description
LEFT JOIN products ON products_to_categories.products_id = products_description.products_id = products.products_id" . "WHERE categories_id = '$categories_id'";

My error message refers to a syntax error near 'categories_id = '1'' at line 1.

I'd much appreciate any help,

Derek

cameraman

7:57 am on Apr 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm not very good at joins, but one thing I see off the bat is in your last concatenation you need a space between products_id and WHERE:
products_id" . " WHERE

scriptmasterdel

12:23 pm on Apr 21, 2007 (gmt 0)

10+ Year Member



Each Join should have an on clause

e.g.

select * from table
LEFT JOIN table2 ON table.field1 = table.field2
LEFT JOIN table3 ON table.field3 = table.field3
WHERE field3 = '{$phpVariable}'

If i was to hazard a guess, this is what i think you are trying to achieve:

$query = "SELECT products_to_categories.*, products_description.*, products.*
FROM products_to_categories
LEFT JOIN products_description ON products_description.products_id = products.products_id
LEFT JOIN products_to_categories ON products_to_categories.products_id = products.products_id
WHERE categories_id = '{$categories_id}'";

Good Luck

derek mcgilvray

2:00 pm on Apr 21, 2007 (gmt 0)

10+ Year Member



Fixed!

I used:
$query="SELECT * FROM products_to_categories
LEFT JOIN products_description ON products_description.products_id = products_to_categories.products_id
LEFT JOIN products ON products.products_id = products_to_categories.products_id
WHERE categories_id = '{$categories_id}'";
mysql_query($query);
$result=mysql_query($query);

and it worked perfectly.

Thanks for your help (again).