Problem with nested SQL
I have a problem with nested select in MySQL.
This one works, but I need to get two of them.
Example:
SELECT `a`.`title` , `a`.`askprice` , `a`.`picture`
, `a`.`description` , `a`.`userid` , `a`.`id`
FROM (
`mm_ads_fields_values` AS afv
)
LEFT JOIN `mm_ads` AS a ON `a`.`id` = `afv`.`aid`
WHERE `afv`.`value` = '38'
AND `a`.`category` = '227'
AND `a`.`status` =1
AND a.id
IN (
SELECT a.id
FROM mm_ads AS a
LEFT JOIN mm_ads_fields_values AS afv ON afv.aid = a.id
WHERE afv.value = '2913'
)
ORDER BY `id` DESC
LIMIT 20
This one works. But I need SQL statement like this one:
SELECT `a`.`title` , `a`.`askprice` , `a`.`picture`
, `a`.`description` , `a`.`userid` , `a`.`id`
FROM (
`mm_ads_fields_values` AS afv
)
LEFT JOIN `mm_ads` AS a ON `a`.`id` = `afv`.`aid`
WHERE `afv`.`value` = '38'
AND `a`.`category` = '227'
AND `a`.`status` =1
AND a.id
IN (
SELECT a.id
FROM mm_ads AS a
LEFT JOIN mm_ads_fields_values AS afv ON afv.aid = a.id
WHERE afv.value = '2913'
)
AND a.id
IN (
SELECT a.id
FROM mm_ads AS a
LEFT JOIN mm_ads_fields_values AS afv ON afv.aid = a.id
WHERE afv.value = '51'
)
ORDER BY `id` DESC
LIMIT 20
And this one, last one, won't wor开发者_如何学Pythonk. Its loading, loading and never nothing happen..
What am I doing wrong?
Regards, Mario
Sorry for my bad English..
Glib answer: shouldn't the second IN statement use OR instead of AND? (edit: and use correct operator precedence.)
...
AND
( a.id
IN (
SELECT a.id
FROM mm_ads AS a
LEFT JOIN mm_ads_fields_values AS afv ON afv.aid = a.id
WHERE afv.value = '2913'
)
OR a.id
IN (
SELECT a.id
FROM mm_ads AS a
LEFT JOIN mm_ads_fields_values AS afv ON afv.aid = a.id
WHERE afv.value = '51'
)
)
...
精彩评论