sequence

This article mainly shows how to use mysql multi-column combination query

What is a multi-column combined query? The value of the query is no longer the value of a single column, but the value of the combined column. For example where (column1,column2) in ((A1,b1),(a2, B2),(a3,b3))

The instance

Build table

create table t_demo(
   id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   name varchar(10),
   score int
);

insert into t_demo(name,score) values('a',10);
insert into t_demo(name,score) values('b',20);
insert into t_demo(name,score) values('c',30);
insert into t_demo(name,score) values('d',40);
insert into t_demo(name,score) values('d',50);
insert into t_demo(name,score) values('e',60);
Copy the code

Multi-column IN query

select * from t_demo where (name,score) in (('c',30),('e',60));
+----+------+-------+
| id | name | score |
+----+------+-------+
| 3  | c    | 30    |
| 6  | e    | 60    |
+----+------+-------+
2 rows in set
Time: 0.112s
Copy the code

Multiple columns = query

select * from t_demo where (name,score) = ('c',30) or (name,score) = ('e',60);
+----+------+-------+
| id | name | score |
+----+------+-------+
| 3  | c    | 30    |
| 6  | e    | 60    |
+----+------+-------+
2 rows in set
Time: 0.119s
Copy the code

summary

Multi-column combined queries are usually rare, and at first glance they are quite magical.

doc

  • mysql-filtering-by-multiple-columns
  • selecting-where-two-columns-are-in-a-set