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
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
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.
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...
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 |
+----+--------+-----+
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.