简体   繁体   中英

SQL Multiple condition on single field

Hi Can someone help me about sql

i have this data in my Table:

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

how can i get the date_added result with the location in 'Manila' , 'Japan', 'Cebu' and has status =1

The three data must exist before i can get the date.

Result should be: based on this table

date_added 
 2012-08-01
 2012-08-06

since on 2012-08-04 'Japan' did not exists.

My Current SAMPLE SQL:

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

Please help.....any help will greatly appreciated

try this:

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

You need the SQL IN operator:

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

Or alternatively, you need to use the OR operator and bracket the location statements:

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

Try this

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

or

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM