简体   繁体   English

我的SQL语句有什么问题?

[英]What is wrong with my SQL Statement?

I'm making a site where the user can select a rental property that has two fields they can select from, Furnished and Pets. 我正在创建一个网站,用户可以在其中选择一个出租物业,该物业有两个字段可供选择,分别是Furnished和Pets。 The options in both select boxes is Yes and No. 两个选择框中的选项是“是”和“否”。

<select name="furnished">
  <option value="">
  <option value="yes">Yes</option>
  <option value="no">No</option>
</select>

<select name="pets">
  <option value="">
  <option value="yes">Yes</option>
  <option value="no">No</option>
</select>

I am writing a SQL statement based on what the user picks in those fields. 我正在根据用户在这些字段中选择的内容编写一条SQL语句。

$sql = 'SELECT *
       FROM properties
       WHERE num_bedrooms >= ' . $_GET['num_bedrooms'] . 
       ' AND num_bathrooms >= ' . $_GET['num_bathrooms'];

if($_GET['furnished'] == 'yes') { //if the furnished is set to yes
  $sql .= ' AND furnished = "yes" OR furnished = "partially"';
} else if($_GET['furnished'] == 'no') { //if the furnished is set to no
  $sql .= ' AND furnished = "no" OR furnished = "description"';
} 

if($_GET['pets'] == 'yes') { //if the pets is set to yes
  $sql .= ' AND pets = "yes" OR pets = "cats" OR pets = "dogs"';
} else if($_GET['pets'] == 'no') { //if the pets is set to no
  $sql .= ' AND pets = "no" OR pets = "description"';
}

If the user chooses yes for furnished, I want it to display all properties that are furnished (yes) OR that are partially furnished(partially). 如果用户为家具选择“是”,我希望它显示所有家具(是)或部分(部分)装饰的属性。 If the user chooses No, I want it to display all properties that are not furnished(no) OR that have a special description(description). 如果用户选择“否”,我希望它显示所有未提供的属性(否)或具有特殊描述(描述)的属性。

If the user chooses yes for pets, I want it to display all properties that allow all pets (yes) OR that allow only cats(cats) OR that allow only dogs(dogs). 如果用户为宠物选择是,我希望它显示所有允许所有宠物的属性(是)或仅允许猫(猫)或仅允许狗(狗)的属性。 If the user chooses No, I want it to display all properties that don't allow pets(no) OR that have a special description(description). 如果用户选择“否”,我希望它显示所有不允许携带宠物的属性(否)或具有特殊描述(说明)的属性。

As an example, this is the outputted SQL statement if the user chooses Yes on Furnished and Yes on Pets: 例如,如果用户在Furnished上选择Yes,在Pets上选择Yes,这是输出的SQL语句:

SELECT * FROM properties 
WHERE num_bedrooms >= 1 
AND num_bathrooms >= 1 
AND furnished = "yes" OR furnished = "partially" 
AND pets = "yes" OR pets = "cats" OR pets = "dogs"

The problem is that it will currently return results that meet EITHER of the furnished or pets requirements in the where clause, whereas I need it to return results that meet BOTH of the furnished and pets requirements in the where clause. 问题在于,它当前将返回满足where子句中带家具或宠物要求的结果的结果,而我需要它返回满足where子句中带家具和宠物要求的结果的结果。

So it will currently return a result that says has the Furnished set to yes, and the pets set to no. 因此,它当前将返回一个结果,显示Furnished设置为yes,而pets设置为no。 How can I have it return all results with furnished set to yes AND pets set to yes? 如何将提供的家具设置为“是”并将宠物设置为“是”,返回所有结果?

What is wrong with my SQL statement? 我的SQL语句有什么问题?

AND comes before OR in operator precedence, so your query reads like AND在运算符优先级中位于OR之前,因此您的查询显示为

SELECT * FROM properties 
WHERE 
(num_bedrooms >= 1 AND num_bathrooms >= 1 AND furnished = "yes")
OR (furnished = "partially" AND pets = "yes")
OR pets = "cats" 
OR pets = "dogs"

which is not quite what you expected. 这与您的预期不符。 Try 尝试

SELECT * FROM properties 
WHERE num_bedrooms >= 1 
AND num_bathrooms >= 1 
AND (furnished = "yes" OR furnished = "partially")
AND (pets = "yes" OR pets = "cats" OR pets = "dogs")

or even better 甚至更好

SELECT * FROM properties 
WHERE num_bedrooms >= 1 
AND num_bathrooms >= 1 
AND furnished IN ("yes", "partially")
AND pets IN ("yes", "cats", "dogs")

You shouldn't do SELECT * try naming the columns you need! 您不应该执行SELECT *尝试命名所需的列!

Put parens around the various OR 'd things. 在各种“ OR ”事物周围放上括号。

$sql .= ' AND (furnished = "yes" OR furnished = "partially")';

$sql .= ' AND (pets = "yes" OR pets = "cats" OR pets = "dogs")';

et cetera. 等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM