1

I have database structure like one shown in following image. My table name is compare_analytics

database structure

I know how to check a single value against multiple columns. Example like one below:

SELECT * FROM `compare_analytics` WHERE '1MB20D-060' IN (igbt1,igbt2,igbt3)

I want to check multiple columns against multiple values. Like, I want to select all rows in which IRG4BC30FD and IRG4BC30FD igbt are present their position in row doesn't matter. They can be in igbt1, igbt2 or igbt3 on any of the column. Query should select rows with id 4,5, and 6 ideally because these values are present in these rows.

I am looking for a query without OR clause, I know that can be achieved with OR clause, but there should be some shorter way to do this.

6
  • because of how your table is set up, there is no other way to do that. You have to use the OR clause. If, however, you redesign your table and data, then you will have other options. As Strawberry mentioned, look up SQL Table Normalization Jul 15, 2014 at 17:55
  • No other way...unless you do some dynamic sql, which gets REALLY messy and is most likely not the way you want to go. Jul 15, 2014 at 17:56
  • What shorter way can be used given that there are three separate and different columns to check? As has already been mentioned, changing the database structure is the only viable way of changing the 'lookup'. Jul 15, 2014 at 17:56
  • Can you please tell me how my table structure should be ?
    – Ankit
    Jul 15, 2014 at 18:00
  • How should I write solution using OR clause ?
    – Ankit
    Jul 15, 2014 at 18:51

1 Answer 1

0
 DROP TABLE IF EXISTS compare_analytics;

 CREATE TABLE compare_analytics
 (igbt       VARCHAR(20) NOT NULL
 ,type INT NOT NULL
 ,PRIMARY KEY(igbt,type)
 );

 INSERT INTO compare_analytics VALUES
 ('1MB20D-040',1),
 ('1MB20D-050',2),
 ('1MB20D-060',3);

 SELECT * 
   FROM compare_analytics
  WHERE igbt IN ('1MB20D-040','1MB20D-060');

 +------------+------+
 | igbt       | type |
 +------------+------+
 | 1MB20D-040 |    1 |
 | 1MB20D-060 |    3 |
 +------------+------+
2
  • I have a lot of respect, however, are you suggesting that most of the table design in this database is correct and they only need to change this one table? Jul 15, 2014 at 22:20
  • @RyanVincent In the comments above, the OP has requested an example of a normalized design; so I've provided one. That's all.
    – Strawberry
    Jul 15, 2014 at 23:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.