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
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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.