我正在开发一个花坛种植系统,并编写了一个查询 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 和OR
s 被忽略了。
任何帮助将非常感激!