Php Mysql查询无法按条件检索数据

Php Mysql查询无法按条件检索数据,php,mysql,Php,Mysql,我有一个问题。我需要根据某些条件从数据库中检索数据,但在某些情况下它会失败。我在下面解释我的问题 select b.member_id as b_member_id, b.rest_name, b.city, b.proviance, b.postal, b.address, b.country, b.person, b.mobile, b.url, b.status, b.premium, b.image, b.business_phone_no, b.email, b.multipl

我有一个问题。我需要根据某些条件从数据库中检索数据,但在某些情况下它会失败。我在下面解释我的问题

    select b.member_id as b_member_id,
b.rest_name,
b.city,
b.proviance,
b.postal,
b.address,
b.country,
b.person,
b.mobile,
b.url,
b.status,
b.premium,
b.image,
b.business_phone_no,
b.email,
b.multiple_image,
b.latitude,
b.longitude,
b.quadrant,
d.member_id as d_member_id,
d.day_id,
d.cat_id,
d.subcat_id,
d.comment,
d.city,
d.special_images,
c.cat_id,
c.special,
sub.subcat_id,
sub.subcat_name,
sub.status,
sl.day_id,
sl.member_id,
sl.date_from,
sl.date_to 
from db_restaurant_basic as b left join db_restaurant_detail as d on b.member_id=d.member_id
left join db_category as c on d.cat_id=c.cat_id 
left join db_subcategory as sub on d.subcat_id=sub.subcat_id
left join db_special_images as sl on d.day_id=sl.day_id 
where b.city='2' and d.day_id='4' and c.special='1' 
and (((sl.date_from IS NULL or sl.date_from='') and (sl.date_to IS NULL or sl.date_to='')) or( sl.date_from <='2016-10-27' and sl.date_to >= '2016-10-27' )) 
and b.status=1 
and sub.status=1 group by d.subcat_id ORDER BY b_member_id DESC
选择b.member\u id作为b\u member\u id,
b、 你的名字,
b、 城市,
b、 省,
b、 邮政,,
b、 地址:,
b、 国家,,
b、 人,,
b、 流动的,
b、 网址,
b、 地位,
b、 溢价,
b、 形象,,
b、 商务电话号码,
b、 电子邮件,
b、 多重图像,
b、 纬度,
b、 经度,
b、 象限,
d、 成员id作为d成员id,
d、 第二天,
d、 猫咪,
d、 子类别id,
d、 评论,,
d、 城市,
d、 特别图片,
c、 猫咪,
c、 特别的,
sub.subcat_id,
sub.subcat_名称,
副主席身份,
sl.day_id,
sl.member_id,
自年月日起,
sl.date_to
从db_restaurant_basic作为b左加入db_restaurant_细节作为d在b.member_id=d.member_id上
左连接db_类别为d上的c。cat_id=c.cat_id
在d.subcat\u id=sub.subcat\u id上将db\u子类别作为子类别左连接
将db_特殊_图像作为sl在d.day_id=sl.day_id左连接
其中b.city='2'和d.day_id='4'和c.special='1'
和(((sl.date_-from为空或sl.date_-from='')和(sl.date_-to为空或sl.date_-to='')或(sl.date_-from='2016-10-27'))
b.状态=1
和sub.status=1按d.subcat\u id排序按b\u成员\u id描述分组

这里我的问题是,一些不符合条件的值也将出现。这里有
b.city='2'
但是也有一些
city=0的值。在这里,我需要的值应根据适当的匹配来。请帮助我。

使用回答窗口的格式选项

 WHERE b.city = 2 
   AND d.day_id = 4   -- NOTE THAT THIS IS AN INNER JOIN!
   AND c.special = 1  -- AND SO IS THIS !!

使用“应答”窗口的格式选项

 WHERE b.city = 2 
   AND d.day_id = 4   -- NOTE THAT THIS IS AN INNER JOIN!
   AND c.special = 1  -- AND SO IS THIS !!

您正在从
db\u restaurant\u basic
表中选择
d.city
,但您在
db\u restaurant\u detail
表中设置的条件是
b.city='2'

因此,城市为0的任何结果都将显示
d
/
db\u餐厅\u详情
表中的城市

如果还需要对其进行筛选,则需要添加
和d.city=2


您可能应该检查是否可以更规范化数据库结构,以避免在不同的表中使用相同的数据。

您正在从
db\u restaurant\u basic
表中选择
d.city
,但您设置的条件是
db\u restaurant\u detail
表中的
b.city='2'

因此,城市为0的任何结果都将显示
d
/
db\u餐厅\u详情
表中的城市

如果还需要对其进行筛选,则需要添加
和d.city=2


您可能应该检查是否可以更规范化您的数据库结构,以避免在不同的表中有相同的数据。

由于选择列表中有两个城市(b.city和d.city),我必须假设您提到的
city='0'
值实际上是
d.city='0'
。为了确保d.city也是“2”,您可以在where子句中添加一个附加条件,或者像这样指定联接

选择。。。从db_restaurant_basic作为b左加入db_restaurant_详细信息作为d上的d。member_id=d.member_id和b.city=d.city左加入…

甚至像这样,摆脱模棱两可


选择。。。从db_restaurant_basic as b left join db_restaurant_detail as d使用(成员id,城市)left join…

由于您在选择列表中有两个城市(b.city和d.city),我必须假设您提到的
city='0'
值实际上是
d.city='0'
。为了确保d.city也是“2”,您可以在where子句中添加一个附加条件,或者像这样指定联接

选择。。。从db_restaurant_basic作为b左加入db_restaurant_详细信息作为d上的d。member_id=d.member_id和b.city=d.city左加入…

甚至像这样,摆脱模棱两可


选择。。。从db_restaurant_basic as b left join db_restaurant_detail as d使用(成员id,城市)left join…

您的
WHERE
子句中有一个
部分。这可能导致这种情况。读取布尔逻辑:且优先级高于或。@BartFriederichs:
((sl.date_-from为NULL或sl.date_-from='')和(sl.date_-to为NULL或sl.date_-to='')或(sl.date_-from='2016-10-27')
。此条件在一个括号内,我需要它。1。城市字段的数据类型是什么?是数字的吗?如果是,则不要使用字符串进行比较。2.你能提供一些样品输出,输出不是你期望的吗?这将帮助我们缩小出错的范围。(为什么sl.date\u from会为空?@Shadow:是的,它的int(11)。您的
where
子句中有
部分。这可能导致这种情况。读取布尔逻辑:且优先级高于或。@BartFriederichs:
((sl.date_-from为NULL或sl.date_-from='')和(sl.date_-to为NULL或sl.date_-to='')或(sl.date_-from='2016-10-27')
。此条件在一个括号内,我需要它。1。城市字段的数据类型是什么?是数字的吗?如果是,则不要使用字符串进行比较。2.你能提供一些样品输出,输出不是你期望的吗?这将帮助我们缩小出错的范围。(为什么sl.date\u from会为空?@Shadow:是的,它的int(11)。