Php MYSQL查询未正确显示数据

Php MYSQL查询未正确显示数据,php,mysql,Php,Mysql,我正在按所选值执行筛选我的数据库的外观如下: contactgroup media gender age isdelete married children driverslicens restatus retype ----------------------------------------------------------- contactgroup1 SMS Male 28 0 yes yes

我正在按所选值执行筛选我的数据库的外观如下:

contactgroup  media  gender  age  isdelete  married  children driverslicens restatus retype
        -----------------------------------------------------------
        contactgroup1     SMS        Male     28     0   yes   yes     yes   owner    apart           
        contactgroup1     SMS        Female   26     0   no    null    no     rent  house
        contactgroup2     SMS        Male     32     0   null   no      null    owner  null
        contactgroup2     SMS        Male     38     0   yes    yes     no      null   null
这是我的疑问:

SELECT * FROM contact
  where isdeleted = 0
    AND contactgroup in ('Contactgroup1', '')
    and media = 'sms'
    AND (`gender` = 'female' OR `gender` = 'male' OR `gender` is null)
    AND (`married` = 'yes' OR `married` = 'no' OR `married` is null)
    AND (`children` = 'yes' OR `children` = 'no' OR `children` is null)
    AND (`driverslicense` = 'yes' OR `driverslicense` = 'no' OR `driverslicense` is null)
    AND (`retype` = 'owner' OR `retype` = 'renting' OR `retype` is null)
    AND (`restatus` = 'apart' OR `restatus` = 'house' OR `restatus` is null)
    and age BETWEEN 18 AND 60 

此查询应显示有关联系人组1的数据,但它显示了所有四个数据。有人能告诉我为什么它显示了我出错的所有数据吗?

您需要组织您的查询和组或条件

SELECT 
  * 
FROM
  contact 
WHERE isdeleted = 0 
  AND contactgroup IN ('Contactgroup1', '') 
  AND media = 'sms' 
  AND (
    `gender` = 'female' 
    OR `gender` = 'male' 
    OR `gender` = 'null'
  ) 
  AND age BETWEEN 18 
  AND 60 
编辑为问题将使用新列更新

您需要使用
IS null

SELECT 
  * 
FROM
  contact 
WHERE isdelete = 0 
  AND contactgroup IN ('Contactgroup1', '') 
  AND media = 'sms' 
  AND (
    `gender` = 'female' 
    OR `gender` = 'male' 
    OR `gender` = 'null'
  ) 
  AND (
    `married` = 'yes' 
    OR `married` = 'no' 
    OR `married` IS NULL
  ) 
  AND age BETWEEN 18 
  AND 60 

您需要组织查询和组或条件

SELECT 
  * 
FROM
  contact 
WHERE isdeleted = 0 
  AND contactgroup IN ('Contactgroup1', '') 
  AND media = 'sms' 
  AND (
    `gender` = 'female' 
    OR `gender` = 'male' 
    OR `gender` = 'null'
  ) 
  AND age BETWEEN 18 
  AND 60 
编辑为问题将使用新列更新

您需要使用
IS null

SELECT 
  * 
FROM
  contact 
WHERE isdelete = 0 
  AND contactgroup IN ('Contactgroup1', '') 
  AND media = 'sms' 
  AND (
    `gender` = 'female' 
    OR `gender` = 'male' 
    OR `gender` = 'null'
  ) 
  AND (
    `married` = 'yes' 
    OR `married` = 'no' 
    OR `married` IS NULL
  ) 
  AND age BETWEEN 18 
  AND 60 

查询中的条件需要在backet中弹出,您希望在搜索中应用一些基本逻辑以外的内容:

SELECT 
    * 
FROM 
    contact 
where 
    isdeleted = 0 
    AND contactgroup in ('Contactgroup1', '') 
    and media = 'sms' 
    AND
    ( 
        `gender` = 'female' 
        OR `gender` = 'male' 
        OR `gender` = 'null' 
    )
    and age BETWEEN 18 AND 60 
任何引入
语句的where子句基本上都会导致数据库忽略任何其他AND语句并返回任何匹配的数据

在你的询问中,你有

OR `gender` = 'male' 
OR `gender` = 'null' 

这基本上意味着返回与此数据匹配的任何行。查询中的更改现在使得必须满足所有其他
语句-只要括号内至少满足一个
语句。

查询中的条件需要在后台弹出,您希望在搜索中应用一些非基本逻辑:

SELECT 
    * 
FROM 
    contact 
where 
    isdeleted = 0 
    AND contactgroup in ('Contactgroup1', '') 
    and media = 'sms' 
    AND
    ( 
        `gender` = 'female' 
        OR `gender` = 'male' 
        OR `gender` = 'null' 
    )
    and age BETWEEN 18 AND 60 
任何引入
语句的where子句基本上都会导致数据库忽略任何其他AND语句并返回任何匹配的数据

在你的询问中,你有

OR `gender` = 'male' 
OR `gender` = 'null' 

这基本上意味着返回与此数据匹配的任何行。查询中的更改现在使得必须满足所有其他
语句-只要括号内至少满足一个
语句。

使用括号括住
-条件如下:

SELECT 
  *
FROM 
  contact 
WHERE isdeleted = 0
AND contactgroup in ('Contactgroup1', '')
AND media = 'sms' 
AND (gender = 'female' OR gender = 'male' OR gender = 'null')
AND age BETWEEN 18 AND 60 
这被称为-
,并且
更重要,因此,如果不使用括号,就好像在这样做:

SELECT 
  *
FROM 
  contact
WHERE (isdeleted = 0 AND contactgroup in ('Contactgroup1', '') and media = 'sms' AND gender = 'female')
OR (gender = 'male')
OR (gender = 'null' AND age BETWEEN 18 AND 60)

使用括号括住
-条件如下:

SELECT 
  *
FROM 
  contact 
WHERE isdeleted = 0
AND contactgroup in ('Contactgroup1', '')
AND media = 'sms' 
AND (gender = 'female' OR gender = 'male' OR gender = 'null')
AND age BETWEEN 18 AND 60 
这被称为-
,并且
更重要,因此,如果不使用括号,就好像在这样做:

SELECT 
  *
FROM 
  contact
WHERE (isdeleted = 0 AND contactgroup in ('Contactgroup1', '') and media = 'sms' AND gender = 'female')
OR (gender = 'male')
OR (gender = 'null' AND age BETWEEN 18 AND 60)

感谢您的回复,它可以正常工作,但我添加了新的Column请检查我的EDIted代码,这次查询没有显示任何数据hi@M M Khalid Junaid,我发现还有一个问题我添加了更多Column这是我的新查询选择*来自联系人,其中isdeleted=0,contactgroup in('Contactgroup1','')和媒体='sms',和(
gender
=“femal”或
gender
为空)和(
marted
=“yes”或
marted
=“no”或
marted
为空)和(
children
=“yes”或
children
=“no”或
children
为空)和(
driverslicense
=“是”或
driverslicense
=“否”或
driverslicense
为空)和(
restatus
=“分开”或
restatus
为空)年龄在18岁到18岁之间60@Xavi在fiddle中查看我的评论您对值使用了错误的列名,也看到了希望您得到了我的观点感谢您的回复它是有效的,但是我添加了新的列,请检查我的EDITED代码,这次这个查询没有显示任何数据hi@M Khalid Junaid,还有一个问题我发现我添加了更多的列这是我的新列查询选择*从isdeleted=0、contactgroup in('Contactgroup1','')和media='sms'和(
gender
='Femal'或
gender
='male'或
gender
为空)和(
已婚
='yes'或
已婚
='no'或
已婚
为空)和(
children
='yes'或
children
为空)和(
driverslicense
='yes'或
driverslicense
='no'或
driverslicense
为空)和(
restatus
restatus
='house'或
restatus
为空)年龄在18岁到18岁之间60@Xavi请参阅我在fiddle中的评论,您对值使用了错误的列名,同时请参阅希望您理解我的观点