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
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.
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);
Yes, it is possible to use MySQL GROUP BY clause with multiple columns just as we can use MySQL DISTINCT clause.
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.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With