Jasmine Vismitananda
Jasmine Vismitananda

Reputation: 45

Find if Row has two given values with MySQL and PHP

I'm looking for an easy way to find if two given numbers are in the same row, without using WHERE and OR in MySQL.

The table schema is like this

Field1 | Field2 | Field3 | Field4 | Field5 | Field6
  0        0        3        0        0        4
  1        5        0        0        0        0
  0        0        2        0        6        0

For example, I would like to know if 3 and 6 are in the same row, and return TRUE or FALSE.

I am looking for something like this pesudo code: SELECT ID FROM Table WHERE :mynumber AND :myothernumber is :inthesamerow?

Thank you!

Upvotes: 1

Views: 64

Answers (2)

Alex
Alex

Reputation: 17289

http://sqlfiddle.com/#!9/93bb8e/2

SELECT *
FROM table
WHERE (Field1=3 OR Field2=3 OR Field3=3 OR Field4=3 OR Field5=3 OR Field6=3  )
   AND (Field1=6 OR Field2=6 OR Field3=6 OR Field4=6 OR Field5=6 OR Field6=6  )

Upvotes: 1

sanderbee
sanderbee

Reputation: 703

You can use the mysql IN commando

SELECT * FROM table WHERE 'valueA' IN(field1, field2, field3, field4, ..) AND 'valueB' IN(field1, field2, field3, field4, ..);

Upvotes: 1

Related Questions