Jump to content

Archived

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

embsupafly

MySQL subquery help/alternative

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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

×

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.