开发者

Translate MySQL query - am I interpreting this correctly?

开发者 https://www.devze.com 2023-03-05 10:23 出处:网络
I am just trying to translate a section of a query: ...OR... AND Zip.id IN (SELECT plan_id FRO开发者_如何转开发M plans_zips

I am just trying to translate a section of a query:

...OR... AND Zip.id IN (SELECT plan_id FRO开发者_如何转开发M plans_zips 
WHERE zip_id = (SELECT id FROM zips WHERE title = '" . $Zip . "'))

From what I can tell the query is saying this:

Get all Zip.ids (from zips table) and get plan_id (from plan_zips table) WHERE zip_id (using plans_zips) = zip.id where the full zip (title) matches the var $Zip.


what you're suggesting is right.

For MySQL it's much more optimal to use joins in place of nested sub-queries like this. The optimizer will be unable to optimize sub-queries and they must be run with the deepest one first.


Not quite.

If you indent the SQL a bit, it will become clearer.

...OR... AND 
Zip.id IN 
    (SELECT plan_id FROM plans_zips  WHERE zip_id =        //Get all the plan_ids where
        (SELECT id FROM zips WHERE title = '" . $Zip . "'))//the zip_id is the value returned from this query.

I agree with James C though - joins are much better and easier to read

0

精彩评论

暂无评论...
验证码 换一张
取 消