1

我正在开发一个花坛种植系统,并编写了一个查询 8 个表的 SQL 语句,以返回适合植物的列表。

表结构: http://pastebin.com/0DUYQis1【从phpmyadmin输出】

示例数据: http: //gyazo.com/13dad0b3370b5053dbcad2a32013a54a / http://gyazo.com/1bd313adb3e8ccd01354c979e69fd059

SQL 语句:

SELECT Plant.Plant_ID, Plant.Plant_Quantity, Plant.Plant_Price, Plant.Plant_Description, Plant.Plant_Latin_Name, Plant.Plant_Common_Name, Plant.Plant_Height, Plant.Plant_Spread, Plant.Plant_Type, Plant.Plant_Family, Plant.Plant_Picture, Plant_Aspect.Plant_Aspect, Plant_Flower_Colour.Plant_Flower_Colour, Plant_Flower_Colour.Plant_Season, Plant_Foliage_Colour.Plant_Foliage_Colour, Plant_Foliage_Colour.Plant_Season, Plant_Moisture.Plant_Moisture, Plant_Soil_PH.Plant_Soil_PH, Plant_Soil_Type.Plant_Soil_Type, Plant_Sun_Type.Plant_Sun_Type 

FROM Plant 

INNER JOIN Plant_Aspect ON Plant.Plant_ID = Plant_Aspect.Plant_ID 
INNER JOIN Plant_Flower_Colour ON Plant.Plant_ID = Plant_Flower_Colour.Plant_ID 
INNER JOIN Plant_Foliage_Colour ON Plant.Plant_ID = Plant_Foliage_Colour.Plant_ID 
INNER JOIN Plant_Moisture ON Plant.Plant_ID = Plant_Moisture.Plant_ID
INNER JOIN Plant_Soil_PH ON Plant.Plant_ID = Plant_Soil_PH.Plant_ID 
INNER JOIN Plant_Soil_Type ON Plant.Plant_ID = Plant_Soil_Type.Plant_ID 
INNER JOIN Plant_Sun_Type ON Plant.Plant_ID = Plant_Sun_Type.Plant_ID 

WHERE Plant_Aspect.Plant_Aspect = 'East-facing'
OR Plant_Aspect.Plant_Aspect = 'Any'
AND Plant_Soil_Type.Plant_Soil_Type = 'Sand' 
OR Plant_Soil_Type.Plant_Soil_Type = 'Any'
AND Plant_Moisture.Plant_Moisture = 'Well-drained' 
OR Plant_Moisture.Plant_Moisture = 'Any'
AND Plant_Soil_PH.Plant_Soil_PH = 'Acid'
OR Plant_Soil_PH.Plant_Soil_PH = 'Any'
AND Plant_Foliage_Colour.Plant_Foliage_Colour = 'Green'
AND Plant_Foliage_Colour.Plant_Season = 'Winter'
OR Plant_Foliage_Colour.Plant_Season = 'Any'
AND Plant_Flower_Colour.Plant_Flower_Colour = 'Orange'
AND Plant_Flower_Colour.Plant_Season = 'Winter'
OR Plant_Flower_Colour.Plant_Season = 'Any'

GROUP BY Plant_ID;

我希望查询只返回那些记录与搜索条件匹配的植物,即。只有那些可以在“冬季”或“任何”季节种植的植物,但它是不应该种植的植物。

例如: http: //gyazo.com/d2ca989fc6def79854a1d9b11e4bc310 -表Plant_ID: 1中仅包含一条记录Plant_Flower_Colour- 春季期间的深蓝色。为什么这个记录被退回了?就好像尾随AND的 s 和ORs 被忽略了。

任何帮助将非常感激!

4

5 回答 5

3

您需要使用括号对您的 where 条件进行分组。

WHERE 
(Plant_Aspect.Plant_Aspect = 'East-facing'
OR Plant_Aspect.Plant_Aspect = 'Any')
AND (Plant_Soil_Type.Plant_Soil_Type = 'Sand' 
OR Plant_Soil_Type.Plant_Soil_Type = 'Any')...
于 2013-02-14T01:45:02.043 回答
3

和/或的另一种形式是使用 IN() 子句,它更易于阅读、更易于编辑且不易发生意外。

WHERE Plant_Aspect.Plant_Aspect IN( 'East-facing' , 'Any' )
AND Plant_Soil_Type.Plant_Soil_Type IN( 'Sand' , 'Any' )
AND Plant_Moisture.Plant_Moisture IN( 'Well-drained' , 'Any' )
AND Plant_Soil_PH.Plant_Soil_PH IN( 'Acid' , 'Any' )
AND Plant_Foliage_Colour.Plant_Foliage_Colour IN( 'Green' )
AND Plant_Foliage_Colour.Plant_Season IN( 'Winter' , 'Any' )
AND Plant_Flower_Colour.Plant_Flower_Colour IN( 'Orange' )
AND Plant_Flower_Colour.Plant_Season IN( 'Winter' , 'Any' )
于 2013-02-14T01:49:10.443 回答
3

我认为您至少需要在 OR 标准周围加上括号:

...
WHERE (Plant_Aspect.Plant_Aspect = 'East-facing'
OR Plant_Aspect.Plant_Aspect = 'Any')
AND (Plant_Soil_Type.Plant_Soil_Type = 'Sand' 
OR Plant_Soil_Type.Plant_Soil_Type = 'Any')
...
于 2013-02-14T01:42:08.980 回答
2

尝试在 OR 比较周围放置括号。

WHERE (Plant_Aspect.Plant_Aspect = 'East-facing'
OR Plant_Aspect.Plant_Aspect = 'Any')
AND (Plant_Soil_Type.Plant_Soil_Type = 'Sand' 
OR Plant_Soil_Type.Plant_Soil_Type = 'Any')
AND (Plant_Moisture.Plant_Moisture = 'Well-drained' 
OR Plant_Moisture.Plant_Moisture = 'Any')
AND (Plant_Soil_PH.Plant_Soil_PH = 'Acid'
OR Plant_Soil_PH.Plant_Soil_PH = 'Any')
AND Plant_Foliage_Colour.Plant_Foliage_Colour = 'Green'
AND (Plant_Foliage_Colour.Plant_Season = 'Winter'
OR Plant_Foliage_Colour.Plant_Season = 'Any')
AND Plant_Flower_Colour.Plant_Flower_Colour = 'Orange'
AND (Plant_Flower_Colour.Plant_Season = 'Winter' OR Plant_Flower_Colour.Plant_Season = 'Any')
于 2013-02-14T01:43:57.760 回答
2

WHERE 子句中的分组是错误的——对于普通读者来说是模棱两可的。AND 绑定比 OR 更紧密,因此您需要使用括号向 DBMS 表明您的意图:

WHERE (Plant_Aspect.Plant_Aspect = 'East-facing'
   OR  Plant_Aspect.Plant_Aspect = 'Any')
  AND (Plant_Soil_Type.Plant_Soil_Type = 'Sand' 
   OR  Plant_Soil_Type.Plant_Soil_Type = 'Any')
  AND (Plant_Moisture.Plant_Moisture = 'Well-drained' 
   OR  Plant_Moisture.Plant_Moisture = 'Any')
  AND (Plant_Soil_PH.Plant_Soil_PH = 'Acid'
   OR  Plant_Soil_PH.Plant_Soil_PH = 'Any')
  AND  Plant_Foliage_Colour.Plant_Foliage_Colour = 'Green'
  AND (Plant_Foliage_Colour.Plant_Season = 'Winter'
   OR  Plant_Foliage_Colour.Plant_Season = 'Any')
  AND  Plant_Flower_Colour.Plant_Flower_Colour = 'Orange'
  AND (Plant_Flower_Colour.Plant_Season = 'Winter'
   OR  Plant_Flower_Colour.Plant_Season = 'Any')

你写的东西被当作你写的:

WHERE  Plant_Aspect.Plant_Aspect = 'East-facing'
   OR (Plant_Aspect.Plant_Aspect = 'Any'
  AND  Plant_Soil_Type.Plant_Soil_Type = 'Sand')
   OR (Plant_Soil_Type.Plant_Soil_Type = 'Any'
  AND  Plant_Moisture.Plant_Moisture = 'Well-drained')
   OR (Plant_Moisture.Plant_Moisture = 'Any'
  AND  Plant_Soil_PH.Plant_Soil_PH = 'Acid')
   OR (Plant_Soil_PH.Plant_Soil_PH = 'Any'
  AND  Plant_Foliage_Colour.Plant_Foliage_Colour = 'Green'
  AND  Plant_Foliage_Colour.Plant_Season = 'Winter')
   OR (Plant_Foliage_Colour.Plant_Season = 'Any'
  AND  Plant_Flower_Colour.Plant_Flower_Colour = 'Orange'
  AND  Plant_Flower_Colour.Plant_Season = 'Winter')
   OR  Plant_Flower_Colour.Plant_Season = 'Any'

于 2013-02-14T01:42:37.537 回答