[英]Apply mysql limit based on IN condition
I have an update query that updates col1
based on a where condition and limits it to 25. Is there a way combining the following query but the limit applies to the indinvidyula IN
conditions as opposed to the whole query 我有一个更新查询,该查询根据where条件将col1
更新并将其限制为25。是否可以将以下查询组合在一起,但是限制适用于indinvidyula IN
条件,而不是整个查询
UPDATE myTable SET col1 = 'ABC' WHERE col2 = 'foo' LIMIT 25
UPDATE myTable SET col1 = 'ABC' WHERE col2 = 'bar' LIMIT 25
UPDATE myTable SET col1 = 'ABC' WHERE col2 = 'abc' LIMIT 25
UPDATE myTable SET col1 = 'ABC' WHERE col2 = '123' LIMIT 25
UPDATE myTable SET col1 = 'ABC' WHERE col2 = '12a' LIMIT 25
UPDATE myTable SET col1 = 'ABC' WHERE col2 = 'bbv' LIMIT 25
Example: I know the following will not have the desired effect. 示例:我知道以下方法不会达到预期的效果。 But i'd like to implement the below query limit
on each condition within the each 但是我想对每个条件中的每个条件实施以下查询limit
UPDATE myTable SET col1 = 'ABC' WHERE col2 IN('foo','bar','abc','123','12a','bbv') LIMIT 25
you can simply select the first 25 rows by ID and use those IDs to update back to database. 您只需按ID选择前25行,然后使用这些ID更新回数据库即可。
Completely untested code: 完全未经测试的代码:
update table set col1 = '' where id in (select id from table WHERE col2 IN('foo','bar','abc','123','12a','bbv') LIMIT 25)
Unless the list was long, I think I'd be tempted to use UNION for this kind of thing... 除非名单很长,否则我很想将UNION用于此类事情...
DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table
(id SERIAL PRIMARY KEY
,fruit VARCHAR(12) NOT NULL
,val VARCHAR(12) NOT NULL
);
INSERT INTO my_table (fruit,val) VALUES
('orange','foo'),
('orange','foo'),
('orange','bar'),
('orange','foo'),
('orange','foo'),
('apple','bar'),
('apple','foo'),
('apple','bar'),
('apple','foo'),
('orange','foo'),
('apple','bar'),
('apple','bar');
SELECT * FROM my_table;
+----+--------+-----+
| id | fruit | val |
+----+--------+-----+
| 1 | orange | foo |
| 2 | orange | foo |
| 3 | orange | bar |
| 4 | orange | foo |
| 5 | orange | foo |
| 6 | apple | bar |
| 7 | apple | foo |
| 8 | apple | bar |
| 9 | apple | foo |
| 10 | orange | foo |
| 11 | apple | bar |
| 12 | apple | bar |
+----+--------+-----+
12 rows in set (0.24 sec)
SELECT *
FROM
( SELECT * FROM my_table WHERE fruit = 'orange' AND val = 'foo' ORDER BY id LIMIT 3 ) a
UNION
( SELECT * FROM my_table WHERE fruit = 'apple' AND val = 'bar' ORDER BY id LIMIT 3 );
+----+--------+-----+
| id | fruit | val |
+----+--------+-----+
| 1 | orange | foo |
| 2 | orange | foo |
| 4 | orange | foo |
| 6 | apple | bar |
| 8 | apple | bar |
| 11 | apple | bar |
+----+--------+-----+
6 rows in set (0.09 sec)
So... 所以...
UPDATE my_table x
JOIN
( SELECT *
FROM
( SELECT * FROM my_table WHERE fruit = 'orange' AND val = 'foo' ORDER BY id LIMIT 3 ) a
UNION
( SELECT * FROM my_table WHERE fruit = 'apple' AND val = 'bar' ORDER BY id LIMIT 3 )
) y
ON y.id = x.id
SET x.val = 'abc';
Query OK, 6 rows affected (0.01 sec)
Rows matched: 6 Changed: 6 Warnings: 0
SELECT * FROM my_table;
+----+--------+-----+
| id | fruit | val |
+----+--------+-----+
| 1 | orange | abc |
| 2 | orange | abc |
| 3 | orange | bar |
| 4 | orange | abc |
| 5 | orange | foo |
| 6 | apple | abc |
| 7 | apple | foo |
| 8 | apple | abc |
| 9 | apple | foo |
| 10 | orange | foo |
| 11 | apple | abc |
| 12 | apple | bar |
+----+--------+-----+
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.