tags:

views:

74

answers:

4

I've got table with properties of some products:

table properties(
  prop_id int,
  product_id int
)

My question is: is it possible to select properties that match without filtering them from the result? For example:

select property_id from properties WHERE property_id IN(1,3,5);

You get only rows that match 1,3,5. I need all rows but with information of which rows matched my criteria. I don't want to use 'UNION'.

Thanks for help.

+1  A: 

left outer join

select
    parent.name
        ,child.name
    from parent
        left outer join child on parent.familyid=child.familyid

will list all parents, regardless if they have children

KM
Exactly - Thanks a lot
orzech
OP's comment says it all, they didn't want a CASE statement
KM
+6  A: 

I don't have experience with mySQL, but assuming it supports a case statement (or something analagous), you could do this:

select
    prop_id,
    product_id,
    (case when prop_id in (1, 3, 5) then 1 else 0 end) as matches_criteria

from properties
Adam Robinson
+1 mySQL does support the case statement, but you need to end with 'end case'
northpole
Your syntax for MySQL looks fine to me. To the questioner: so with Adam's syntax, you get a new column -- if that column is a 1, then the row matched your your criteria.
artlung
Doh! @birdlips has it right. http://dev.mysql.com/doc/refman/5.0/en/case-statement.html I make a terrible syntax checker. :-)
artlung
why the up votes here? the question isn't worded that well, but they were looking for a left join, not a case.
KM
It's only CASE as a separate statement that needs END CASE. The CASE inside a SELECT ends with END, not END CASE.
Andomar
A: 
SELECT
    IF (property_id IN (1, 3, 5), 1, 0) AS matched
FROM
    properties;
Greg
+2  A: 

You can use a case statement:

SELECT 
  prop_id, 
  product_id, 
  (CASE 
    WHEN property_id IN(1,3,5) THEN 1 
    ELSE 0 
  END)
FROM  properties;

http://dev.mysql.com/doc/refman/5.0/en/case-statement.html