mysql中update和limit_在MySQL中可以将UPDATE查询与
LIMIT⼀起使⽤吗?
是的,可以在MySQL中将UPDATE查询与LIMIT⼀起使⽤。让我们看看如何。
对于我们的⽰例,我们将⾸先创建⼀个表。CREATE命令⽤于创建表。mysql>CREATE table tblUpdateLimit
-> (
-> id int,
-> name varchar(100)
-> );
记录是在INSERT命令的帮助下插⼊的。mysql>INSERT into tblUpdateLimit values(1,'John');
mysql>INSERT into tblUpdateLimit values(2,'Carol');
mysql>INSERT into tblUpdateLimit values(3,'Smith');
mysql>INSERT into tblUpdateLimit values(4,'Kayle');
mysql>INSERT into tblUpdateLimit values(5,'David');
mysql>INSERT into tblUpdateLimit values(6,'Jason');
mysql>INSERT into tblUpdateLimit values(7,'Larry');
mysql>INSERT into tblUpdateLimit values(8,'Serhat');
mysql>INSERT into tblUpdateLimit values(9,'Winny');
要显⽰上表,这是查询。mysql> SELECT *from tblUpdateLimit;
以下是输出。+------+--------+
| id |name |
+------+--------+
| 1 | John |
| 2 | Carol |
| 3 | Smith |
| 4 | Kayle |
| 5 | David |
| 6 | Jason |
| 7 | Larry |
| 8 | Serhat |
| 9 | Winny |
+------+--------+
9 rows in set (0.00 sec)
现在让我们看看使⽤带限制的UPDATE查询的语法。UPDATE yourTableName SET column_name='some value’'mysql 要钱吗
WHERE column_name1 IN (
SELECT column_name1 FROM (
select column_name1 from yourTableName order by column_name1 asc limit integerValue,integerValue)
anyAliasName );
现在实施查询以实现我们的⽬的,并使⽤它来设置名称“ Adam”(限制为7)。mysql> UPDATE tblUpdateLimit SET name = 'Adam' -> WHERE id IN (
SELECT id FROM ( select id from tblUpdateLimit order by id asc limit 0,7)l);
Rows matched: 7 Changed: 7 Warnings: 0
检查表是否更新。mysql> SELECT *from tblUpdateLimit;
这是输出。+------+--------+
| id | name |
+------+--------+
| 1 | Adam |
| 2 | Adam |
| 3 | Adam |
| 4 | Adam |
| 5 | Adam |
| 6 | Adam |
| 7 | Adam |
| 8 | Serhat |
| 9 | Winny |
+------+--------+
9 rows in set (0.00 sec)
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论