SQL单个字段的多个条件

时间:2012-08-06 10:56:02

标签: php mysql sql

嗨,有人可以帮我解决一下sql

我在表格中有这些数据:


date_added | location     |  status
2012-08-01    Manila           1
2012-08-01    Japan            1
2012-08-01    Cebu             1

2012-08-04    Manila           1
2012-08-04    Cebu             1
2012-08-04    Africa           1

2012-08-06    Manila           1
2012-08-06    Japan            1
2012-08-06    Cebu             1


我怎样才能获得date_added结果,其位置在'Manila','Japan','Cebu'并且状态= 1

在我得到日期之前,这三个数据必须存在。

结果应该是:基于此表

date_added 
 2012-08-01
 2012-08-06

因为在2012-08-04'日本'不存在。

My Current SAMPLE SQL:

SELECT date_added FROM TABLE WHERE location ='Manila' AND location ='Japan' 
       AND location ='Cebu' AND STATUS =1;

请帮助.....任何帮助将非常感谢

5 个答案:

答案 0 :(得分:10)

试试这个:

SELECT DATE_ADDED 
FROM TABLE 
WHERE LOCATION IN ('MANILA' , 'JAPAN' , 'CEBU')
AND STATUS =1
GROUP BY DATE_ADDED
HAVING (COUNT(DISTINCT LOCATION)=3)

答案 1 :(得分:2)

您需要SQL IN运算符:

SELECT date_added FROM TABLE WHERE location IN ('Manila', 'Japan', 'Cebu') AND STATUS =1;

或者,您需要使用OR运算符并将位置语句括起来:

SELECT date_added FROM TABLE WHERE ( location ='Manila' OR location ='Japan' OR location ='Cebu' ) AND STATUS =1;

答案 2 :(得分:0)

试试这个

SELECT date_added FROM TABLE WHERE location ='Manila' or location ='Japan'  or location ='Cebu' AND STATUS =1;

select date_added FROM TABLE WHERE location in ('Manila','Japan','Cebu') AND status='1' GROUPED BY date_added

答案 3 :(得分:0)

WHERE location IN ('Manila' , 'Japan', 'Cebu') AND status = 1

答案 4 :(得分:0)

当您在同一字段上使用AND条件时,它总是会导致错误,因为字段只能有1 possible value。因此,您需要使用IN作为

SELECT Date_added 
FROM   myTable
WHERE  `Location` IN ('MANILA' , 'JAPAN' , 'CEBU')
          AND `Status` =1
GROUP BY Date_added 
HAVING   COUNT(DISTINCT Location) = 3