9

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

我的表中有这些数据:

添加日期 | 位置 | 地位
2012-08-01 马尼拉 1
2012-08-01 日本 1
2012-08-01 宿雾 1

2012-08-04 马尼拉 1
2012-08-04 宿雾 1
2012-08-04 非洲 1

2012-08-06 马尼拉 1
2012-08-06 日本 1
2012-08-06 宿雾 1


我怎样才能在“马尼拉”、“日本”、“宿务”的位置获得 date_added 结果并且状态 = 1

这三个数据必须存在才能获得日期。

结果应该是:基于此表

添加日期
 2012-08-01
 2012-08-06

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

我当前的示例 SQL:

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

请帮助.....任何帮助将不胜感激

4

5 回答 5

11

尝试这个:

SELECT DATE_ADDED 
FROM TABLE 
WHERE LOCATION IN ('MANILA' , 'JAPAN' , 'CEBU')
AND STATUS =1
GROUP BY DATE_ADDED
HAVING (COUNT(DISTINCT LOCATION)=3)
于 2012-08-06T10:59:01.310 回答
2

您需要 SQLIN运算符:

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;
于 2012-08-06T11:00:27.507 回答
0

when you use AND condition on the same field,it will always result to false because a field can only have 1 possible value. So you needto use IN for that

SELECT Date_added 
FROM   myTable
WHERE  `Location` IN ('MANILA' , 'JAPAN' , 'CEBU')
          AND `Status` =1
GROUP BY Date_added 
HAVING   COUNT(DISTINCT Location) = 3
于 2012-08-06T11:13:04.667 回答
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
于 2012-08-06T10:59:28.237 回答
0
WHERE location IN ('Manila' , 'Japan', 'Cebu') AND status = 1
于 2012-08-06T11:00:44.697 回答