SQL Query to retrieve rows having conditional values in a column -
i don't know if question has been asked also. if please direct me link.
i have table has 3 columns name
, type
, date
. type
can 4 values a, b, c , d
i want fetch records of type a, b or c condition should fetch if same name has type of d.
e.g. lets consider table
name type date abc 5/7 abc b 6/7 abc d 7/7 xyz 5/7 xyz d 6/7 lmn 5/7 lmn b 6/7 lmn c 7/7
so deal here need following result set
abc 5/7 abc 6/7 xyz 5/7
because abc , xyz has type
d other records of abc , xyz shown. since lmn not have type
d it's not included in result set.
to test if record exist, can use where exists
:
select * mytable t1 exists ( select * mytable t2 t1.name=t2.name , t2.type="d" );
that's self explanatory here's reference : http://dev.mysql.com/doc/refman/5.0/en/exists-and-not-exists-subqueries.html
if want exclude d records, :
select * mytable t1 t1.type<>"d" , exists ( select * mytable t2 t1.name=t2.name , t2.type="d" );
Comments
Post a Comment