Jump to content

MySQL subquery help/alternative


embsupafly

Recommended Posts

Trying to run the following MySQL query/subquery:

SELECT products_id FROM products_attributes WHERE products_id = (SELECT products_id FROM products WHERE products_model = 'V100');

I am doing this in phpMyAdmin and I am getting back a syntax error.

I am running MySQL version 4.0.25, and thought I heard something a while back that subqueries were not supported until 4.1, but then again, I am not sure, but I don't want to rack my brain out if this isn't going to work wity my version of MySQL.

Should this work in pre 4.1 MySQL? Do I have the wrong syntax? Is there an alternative way to do this.

What I am trying to do with the query is select all products_id from the products_attributes table that match products_ids that are in the products table that correspond to a certain products_model.
Link to comment
https://forums.phpfreaks.com/topic/6148-mysql-subquery-helpalternative/
Share on other sites

You're correct -- no subqueries until v4.1.

Luckily, in your case, you can rewrite this a JOIN -- which is usually better to do anyway, if possible (UNTESTED):

[code]SELECT p.products_id FROM products AS p LEFT JOIN products_attributes AS pa ON ( p.products_id = pa.products_id ) WHERE p.products_model = 'V100';[/code]

Hope that helps.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.