MultiColumn在mysql中的IN

时间:2016-11-01 12:00:01

标签: mysql

我正在运行这样的查询 -

UPDATE table                                                                                                                                                           
SET eligible=1                                                                                                                                                                                             
where (name, age) in [["john",29],["mark",28],["jim",20]]

表结构就像这样

name      age     eligible
mark       28      null
john       29      null
Max        20      null

但是mysql会抛出错误的语法错误,任何人都可以确认在mysql中允许这样做吗?

2 个答案:

答案 0 :(得分:3)

SELECT * FROM my_table ORDER BY id LIMIT 10;
+----+-------+--------+
| id | type  | count  |
+----+-------+--------+
|  1 | green |      4 |
|  2 | blue  |      3 |
|  3 | blue  | 443792 |
|  4 | green | 455353 |
|  5 | blue  | 445389 |
|  6 | blue  | 360885 |
|  7 | green | 468258 |
|  8 | red   | 258636 |
|  9 | blue  | 388405 |
| 10 | green | 166117 |
+----+-------+--------+

 SELECT * 
   FROM my_table 
  WHERE (type,count) IN (('red',1000),('green',2000),('blue',3000)) ORDER BY id LIMIT 100
    -> ;
+--------+-------+-------+
| id     | type  | count |
+--------+-------+-------+
| 137339 | blue  |  3000 |
| 339554 | red   |  1000 |
| 947445 | green |  2000 |
+--------+-------+-------+

请注意,在使用此方法时,MySQL可能会遇到使用索引的问题。出于这个原因,将查询写出“长手”会更有效率。

答案 1 :(得分:0)

如果您使用的是旧版本的MySQL,或者另一种方法是将where语句分开:

UPDATE table                                                                                                                                                           
SET eligible=1                                                                                                                                                                                             
WHERE
(name = "john" AND age = 29) OR
(name = "mark" AND age = 28) OR
(name = "jim" AND age = 20)