I have this simple query and its not getting the data of the category table. Any suggestions ?
SELECT products.*, categories.* FROM products
LEFT JOIN categories ON products.prod_catg_id = categories.catg_id
WHERE categories.catg_status = '1' ORDER BY RAND(), products.prod_price LIMIT 0, 4
With that WHERE condition, the LEFT JOIN becomes an INNER JOIN. So what do you mean by ‘its not getting the data of the category table’ ? If it isn’t getting the category table data, it shouldn’t be getting any data.
If you really need a LEFT JOIN, then try putting the condition in the ON clause instead of in the WHERE clause.
Thanks for your response…i have modified my query:
SELECT products.*, categories.* FROM products
LEFT JOIN categories ON products.prod_catg_id = categories.catg_id
ORDER BY RAND(), products.prod_price LIMIT 0, 4
But even after removing the WHERE clause I am still not getting the data from the categories table but in both the cases the data from products table is coming fine.
Sorry to bother again. I have now a new problem with the same tables. I have now included sub-categroies also in the categories table. I have attached the picture which shows the categories table.
I am executing the following query to get the results but its not returning any data at all.
SELECT products.*, categories.*, categories.* FROM products
LEFT JOIN categories ON products.prod_catg_id = categories.catg_id
LEFT JOIN categories ON products.prod_sbcg_id = categories.catg_id
WHERE categories.catg_status = '1' ORDER BY RAND(), products.prod_price LIMIT 0, 10
I made this simple query as per your guideline but it is not returning any data.
SELECT products.*, categories.*, categories.* FROM products
LEFT JOIN categories AS category ON products.prod_catg_id = category.catg_id
LEFT JOIN categories AS subcategory ON products.prod_sbcg_id = subcategory.catg_id
Removed the double categories but still not working
SELECT products.*, categories.* FROM products
LEFT JOIN categories AS category ON products.prod_catg_id = category.catg_id
LEFT JOIN categories AS subcategory ON products.prod_sbcg_id = subcategory.catg_id
Sorry but I am not understanding it. When I have done like following:
SELECT products.*, categories.* AS maincat, categories.* AS subcat FROM products
LEFT JOIN categories AS maincat ON maincat.catg_id = products.prod_catg_id
LEFT JOIN categories AS subcat ON subcat.catg_id = products.prod_sbcg_id
Even then i am getting error: Unknown table ‘categories’
Also what is bofadem ? Googling it just bring me back to some of your old posts at siteforum.
you cannot assign an alias like that, using the dreaded, evil “select star”
first of all, you should stop using that
secondly, you have to use table aliases like this –
SELECT products.*
, [COLOR="Blue"]maincat[/COLOR].*
, [COLOR="blue"]subcat[/COLOR].*
FROM ...
but even then, your troubles are not over, because both of your tables (maincat and subcat) have the same columns, so you should really assign column aliases to them as well
do a search for threads within this forum for “join same table twice” and i’m sure you will find numerous examples
Thanks very much. After your hints and tips it has worked :D. I am so happy. The final query:
SELECT products.*,
maincat.catg_id AS catgid, maincat.catg_name AS catgname,
subcat.catg_id AS sbcgid, subcat.sbcg_name AS sbcgname FROM products
LEFT JOIN categories AS maincat ON maincat.catg_id = products.prod_catg_id
LEFT JOIN categories AS subcat ON subcat.catg_id = products.prod_sbcg_id