Mysql使用OR选择Statement

时间:2016-04-06 00:03:46

标签: php mysql

我在select语句中很新,所以有人可以帮我处理我想要完成的PHP查询。

MYSQL表格中有5列(Partner_Name,Partner_Name2,Partner_Name3,Partner_Name4,Partner_Name5)

我当前的PHP语句如下所示。

$query = mysqli_query( $connection, "SELECT * FROM tblclients  WHERE tblclients.partner_name =  'Store_1' AND tblclients.package =  'standard' AND tblclients.category = 'all' LIMIT 0, 9"); 

我的目标是拥有这样的东西......

$query = mysqli_query( $connection, "SELECT * FROM tblclients WHERE tblclients.partner_name =  'Store_1' OR tblclients.partner_name_2 =  'Store_1' OR tblclients.partner_name_3 =  'Store_1' OR tblclients.partner_name_4 =  'Store_1' OR tblclients.partner_name_5 =  'Store_1' AND tblclients.package =  'standard' AND tblclients.category = 'all'  0, 9");  

它似乎没有按照我预期的方式返回结果。我做错了吗?

3 个答案:

答案 0 :(得分:1)

试试这个:

$query = mysqli_query( $connection, "SELECT * FROM tblclients WHERE tblclients.package =  'standard' AND tblclients.category = 'all' AND (tblclients.partner_name =  'Store_1' OR tblclients.partner_name_2 =  'Store_1' OR tblclients.partner_name_3 =  'Store_1' OR tblclients.partner_name_4 =  'Store_1' OR tblclients.partner_name_5 =  'Store_1')  0, 9"); 

答案 1 :(得分:1)

试试这个:

$ query = mysqli_query($ connection," SELECT * FROM tblclients WHERE(tblclients.partner_name =' Store_1'或tblclients.partner_name_2 =' Store_1'或tblclients.partner_name_3 =' Store_1'或tblclients.partner_name_4 =' Store_1'或tblclients.partner_name_5 =' Store_1')和tblclients.package ='标准' AND tblclients.category =' all' limit 0,9");

答案 2 :(得分:0)

顺便说一下,你忘了在第二个查询中添加LIMIT。

  

$ query = mysqli_query($ connection," SELECT * FROM tblclients WHERE tblclients.package =' standard' AND tblclients.category =' all' AND(tblclients.partner_name =' Store_1'或tblclients.partner_name_2 =' Store_1'或tblclients.partner_name_3 =' Store_1'或tblclients.partner_name_4 =' Store_1'或tblclients .partner_name_5 =' Store_1')LIMIT 0,9");

如果你有时间,可能会进行表优化,这样你的查询就会更好,更容易阅读。