SELECT *
FROM `SC_products`
WHERE `productID`
IN (
(
SELECT `productID`
FROM `SC_product_options_set`
LEFT JOIN `SC_products_opt_val_variants` ON `SC_product_options_set`.`variantID` = `SC_products_opt_val_variants`.`variantID`
WHERE `SC_products_opt_val_variants`.`optionID` = '14'
GROUP BY `SC_product_options_set`.`productID`
ORDER BY `SC_products_opt_val_variants`.`sort_order` ASC
)
UNION ALL
(
SELECT `productID`
FROM `SC_product_options_values`
WHERE `SC_product_options_values`.`optionID` = '14'
ORDER BY `SC_product_options_values`.`option_value_ru` ASC
)
)
GROUP BY `SC_products`.`productID`
LIMIT 0 , 30
ERROR№1064 - 您的SQL语法出错;查看与您的MySQL服务器版本对应的手册,以便在第7行使用“UNION ALL(SELECT productID
FROM SC_product_options_values
WHERE”SC_produ'附近使用正确的语法
请帮助。
答案 0 :(得分:1)
试试这个:
SELECT *
FROM `sc_products`
WHERE `productid` IN (
SELECT `productid`
FROM `sc_product_options_set`
LEFT JOIN `sc_products_opt_val_variants`
ON `sc_product_options_set`.`variantid` =
`sc_products_opt_val_variants`.`variantid`
WHERE `sc_products_opt_val_variants`.`optionid` = '14'
GROUP BY `sc_product_options_set`.`productid`
UNION ALL
SELECT `productid`
FROM `sc_product_options_values`
WHERE `sc_product_options_values`.`optionid` = '14'
)
GROUP BY `sc_products`.`productid`
LIMIT 0, 30
P.S:你的子查询中不需要ORDER BY,因为它在IN子句中
答案 1 :(得分:0)
之后,您需要将这些结果包装在另一个SELECT层中,这样您的IN比较就可以了。