Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Multiple condition on single field

Tags:

sql

php

mysql

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

like image 310
Monski Avatar asked Aug 06 '12 10:08

Monski


People also ask

How do I pass multiple conditions in SQL query?

You can specify multiple conditions in a single WHERE clause to, say, retrieve rows based on the values in multiple columns. You can use the AND and OR operators to combine two or more conditions into a compound condition. AND, OR, and a third operator, NOT, are logical operators.

How do I SELECT multiple values in one column in SQL?

Note – Use of IN for matching multiple values i.e. TOYOTA and HONDA in the same column i.e. COMPANY. Syntax: SELECT * FROM TABLE_NAME WHERE COLUMN_NAME IN (MATCHING_VALUE1,MATCHING_VALUE2);

Can a single SQL query have multiple group by clauses?

Yes, it is possible to use MySQL GROUP BY clause with multiple columns just as we can use MySQL DISTINCT clause.

How do you write multiple conditions?

You can use the OR condition in the WHERE clause to test multiple conditions where the record is returned if any one of the conditions are met. This example uses the WHERE clause to define multiple conditions, but instead of using the AND condition, it uses the OR condition.


2 Answers

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)
like image 140
Joe G Joseph Avatar answered Oct 03 '22 18:10

Joe G Joseph


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;
like image 29
asc99c Avatar answered Oct 03 '22 17:10

asc99c