gpt4 book ai didi

mysql - SQL 查询不返回条件集

转载 作者:行者123 更新时间:2023-11-29 03:50:07 25 4
gpt4 key购买 nike

我正在开发一个花坛种植系统,并编写了一个查询 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 表中的一条记录 - Spring 为深蓝色。为什么这条记录被退回了?这就好像尾随的 ANDOR 被忽略了。

如有任何帮助,我们将不胜感激!

最佳答案

and/or 的另一种形式是使用 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' )

关于mysql - SQL 查询不返回条件集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14866433/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com