我确实找到了类似问题的答案,但还不能完全理解。
我有一个带轮胎的MySQL桌子。轮胎记录由以下列组成:
当宽度,高度和直径与某些值匹配时,我需要选择某些品牌的轮胎。如果没有,我需要选择其他部分重叠的轮胎品牌。
我需要在bogus-SQL中实现:
SELECT * FROM tyres
IF (width = 275 AND height = 55 AND diameter = 18)
OR (width = 270 AND height = 55 AND diameter = 17)
OR (width = 290 AND height = 45 AND diameter = 19)
THEN WHERE_CLAUSE = "WHERE brand = 'dunlop' OR brand = 'michelin' OR brand = 'vredestein'"
ELSE WHERE_CLAUSE = "WHERE brand = 'goodyear' OR brand = 'michelin' OR brand = 'ferrari'"
我尝试了很多符号如下,但(显然)不断收到语法错误#1064:
SELECT * FROM tyres
CASE WHEN width = '275' AND height = '55' AND diameter = '18' THEN " WHERE brand = 'dunlop' OR brand = 'michelin' OR brand = 'vredestein'"
ELSE " WHERE brand = 'goodyear' OR brand = 'michelin' OR brand = 'ferrari'"
如何根据CASE语句的结果使用特定的WHERE子句?
答案 0 :(得分:1)
尝试UNION ALL
:
SELECT * FROM tyres
WHERE width = 275 AND height = 55 AND diameter = 18
AND (brand = 'dunlop' OR brand = 'michelin' OR brand = 'vredestein')
UNION ALL
SELECT * FROM tyres
WHERE width <> 275 OR height <> 55 OR diameter <> 18
AND (brand = 'goodyear' OR brand = 'michelin' OR brand = 'ferrari')
第一个SELECT
是您帖子中的THEN
部分,第二个SELECT
是ELSE
部分。