Mysql SELECT with an OR across 2 columns

Posted by Haroldo on Stack Overflow See other posts from Stack Overflow or by Haroldo
Published on 2010-03-24T12:35:25Z Indexed on 2010/03/24 12:43 UTC
Read the original article Hit count: 154

Filed under:
|

I'm creating a 'similar items' link table.

i have a 2 column table. both columns contains product ids. The table is showing that these items are similar. However ids in the left column are more valuable.

Say i want to select similar items to product '125b'. i only want 3 similar items to 125b. If there are any instances of 125b in col1 I would prefer these to finding 125b in col2.

so i need a select statement along the lines of

SELECT * FROM similar_items WHERE col_1={$id} OR col_2={$id} ORDER BY column(?) LIMIT 3

i do not want to do 2 separate queries ( ie query 2 if count(query1) <3 )

© Stack Overflow or respective owner

Related posts about mysql

Related posts about php