php 使用OR的Mysql Select语句

qybjjes1  于 2023-03-28  发布在  PHP
关注(0)|答案(4)|浏览(135)

我对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");

它似乎没有以我期望的方式返回结果。我这样做是错误的吗?

x9ybnkn6

x9ybnkn61#

试试这个:

$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");
ia2d9nvy

ia2d9nvy2#

顺便说一句,你忘了在第二个查询中添加LIMIT。
$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')LIMIT 0,9”);
如果你有时间,也许可以优化表,这样你的查询会更好,更容易阅读。

eaf3rand

eaf3rand3#

试试这个:
$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' limit 0,9“);

zaq34kh6

zaq34kh64#

在您的情况下,只需使用(...或...):
AND(tblclients.partner_name = 'Store_1' OR tblclients.partner_name_2 = 'Store_1' OR ...)

相关问题