如题这个问题怎么解决
网上说是因为游标的问题
可是我并没有对这个表建立游标阿
网上说是因为游标的问题
可是我并没有对这个表建立游标阿
解决方案 »
- 将sql server的函数方法转换成MySQL请各位高手指点。
- 如何将Mysql程序及其数据库打包进.NET程序安装包?
- union出来的表可以删除记录么
- 求助。。。select count 的赋值问题
- 通过MYSQL命令行或者PHPMYADMIN插入或显示中文数据时乱码!!
- postgresql qq群 :27831241
- 关于修改表结构.插入一个新的字段到指定的位置,而不是排在最后
- 請問這樣的數據庫連接哪里有下載!
- SQL中含有特殊字符,进行查询时会出错,怎么办?
- php 插入mysql图片的问题,求大神们解答
- [sql] 请教这种sql语句属于哪类连接?
- MY SQL ERROR2003的问题
贴你的记录及要求结果、SQL语句出来
例如表一user
id name
表二stu
id class
因为表一中name可能重复,所以要去重,保留最小的那个id。同时要将表二中的相应的id改变成最小的那个id
drop procedure if exists user_pro;
create procedure user_pro ()
begin
declare xid varchar(10);
select min(id) into xid from user
group by name having count(name)>1; update stu
set id=xid
where id in (select id from user where name in (select name from use where id=xid)); delete from user
where name in (select name from user group by name having count(name)>1) and id!=xid;
end
delete a from b1 a left join
(select name,min(id) as mi from b1 group by name) b
on a.name=b.name and a.id=b.mi where b.name is null
select id from user a where exists (select id from user where name=a.name and id>a.id)
) b on u.id=b.id
或者
select group_concat(min_id) into xid from (sellect name ,min(id) as min_id from user group by name) c
delete from stu where not FIND_IN_SET(id,xid);
delete from user where not FIND_IN_SET(id,xid);
表一 user (含有两列,id以及名字)
id name
1 A
2 B
3 C
4 A
5 C
6 D
7 B
8 E
表二 stu (含有两列,类别以及id)
class id
1 2
2 1
3 3
4 7
5 1
6 8
7 4
8 4
9 5
10 2
在表一中,要求去重,即将name重复的记录删除,保留id最小
在表二中,将表一中去重的记录的id换成相应的最小id
例如,表一中name为A重复,A对应的id有1、4,所以在表一中删除4,而在表二中将id为4的记录修改为1
要求的结果如下:
表一 user
id name
1 A
2 B
3 C
6 D
8 E
表二 stu
class id
1 2
2 1
3 3
4 2
5 1
6 8
7 1
8 1
9 3
10 2
(select name,max(id) as ma,min(id) as mi from b1 group by name having count(*)>=2) c
on b2.id=c.ma
set b2.id=c.miselect name,min(id) from b1 group by name
+----+------+
| id | name |
+----+------+
| 1 | A |
| 2 | B |
| 3 | C |
| 4 | A |
| 5 | C |
| 6 | D |
| 7 | B |
| 8 | E |
+----+------+
8 rows in set (0.00 sec)mysql> delete u from user u,user u1 where u.name=u1.name and u.id>u1.id;
Query OK, 3 rows affected (0.08 sec)mysql> select * from user;
+----+------+
| id | name |
+----+------+
| 1 | A |
| 2 | B |
| 3 | C |
| 6 | D |
| 8 | E |
+----+------+
5 rows in set (0.00 sec)mysql>
然后再 在表一中,要求去重,即将name重复的记录删除,保留id最小mysql> select * from user;
+----+------+
| id | name |
+----+------+
| 1 | A |
| 2 | B |
| 3 | C |
| 4 | A |
| 5 | C |
| 6 | D |
| 7 | B |
| 8 | E |
+----+------+
8 rows in set (0.00 sec)mysql> select * from stu;
+-------+------+
| class | id |
+-------+------+
| 1 | 2 |
| 2 | 1 |
| 3 | 3 |
| 4 | 7 |
| 5 | 1 |
| 6 | 8 |
| 7 | 4 |
| 8 | 4 |
| 9 | 5 |
| 10 | 2 |
+-------+------+
10 rows in set (0.00 sec)mysql> update stu,(
-> select u1.id,min(u2.id) as m_id
-> from user u1,user u2 where u1.name=u2.name
-> group by u1.id) u
-> set stu.id=u.m_id
-> where stu.id=u.id;
Query OK, 4 rows affected (0.06 sec)
Rows matched: 10 Changed: 4 Warnings: 0mysql> select * from stu;
+-------+------+
| class | id |
+-------+------+
| 1 | 2 |
| 2 | 1 |
| 3 | 3 |
| 4 | 2 |
| 5 | 1 |
| 6 | 8 |
| 7 | 1 |
| 8 | 1 |
| 9 | 3 |
| 10 | 2 |
+-------+------+
10 rows in set (0.00 sec)
mysql> delete u from user u,user u1 where u.name=u1.name and u.id>u1.id;
Query OK, 3 rows affected (0.05 sec)mysql> select * from user;
+----+------+
| id | name |
+----+------+
| 1 | A |
| 2 | B |
| 3 | C |
| 6 | D |
| 8 | E |
+----+------+
5 rows in set (0.00 sec)mysql>
(select name,max(id) as ma,min(id) as mi from b1 group by name having count(*)>=2) c
on b2.id=c.ma
set b2.id=c.midelete a from b1a a inner join b1a b
on a.name=b.name and a.id>b.id
又是上个问题The total number of locks exceeds the lock table size
*************************** 1. row ***************************
Type: InnoDB
Name:
Status:
=====================================
090514 16:04:24 INNODB MONITOR OUTPUT
=====================================
Per second averages calculated from the last 55 seconds
----------
SEMAPHORES
----------
OS WAIT ARRAY INFO: reservation count 58653, signal count 58602
Mutex spin waits 0, rounds 168148, OS waits 6887
RW-shared spins 103231, OS waits 51610; RW-excl spins 89, OS waits 86
------------
TRANSACTIONS
------------
Trx id counter 0 4096580
Purge done for trx's n:o < 0 4096009 undo n:o < 0 0
History list length 1
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 0 0, not started, process no 2096, OS thread id 2747169680
MySQL thread id 111, query id 1279 localhost root
show engine innodb status
---TRANSACTION 0 4096570, not started, process no 2096, OS thread id 2701028240
MySQL thread id 98, query id 1129 localhost guoling
---TRANSACTION 0 0, not started, process no 2096, OS thread id 2746968976
MySQL thread id 59, query id 711 172.31.252.169 root
--------
FILE I/O
--------
I/O thread 0 state: waiting for i/o request (insert buffer thread)
I/O thread 1 state: waiting for i/o request (log thread)
I/O thread 2 state: waiting for i/o request (read thread)
I/O thread 3 state: waiting for i/o request (write thread)
Pending normal aio reads: 0, aio writes: 0,
ibuf aio reads: 0, log i/o's: 0, sync i/o's: 0
Pending flushes (fsync) log: 0; buffer pool: 0
62776 OS file reads, 7 OS file writes, 7 OS fsyncs
0.00 reads/s, 0 avg bytes/read, 0.00 writes/s, 0.00 fsyncs/s
-------------------------------------
INSERT BUFFER AND ADAPTIVE HASH INDEX
-------------------------------------
Ibuf: size 1, free list len 0, seg size 2,
0 inserts, 0 merged recs, 0 merges
Hash table size 34679, node heap has 1 buffer(s)
0.00 hash searches/s, 0.00 non-hash searches/s
---
LOG
---
Log sequence number 7 862427748
Log flushed up to 7 862427748
Last checkpoint at 7 862427748
0 pending log writes, 0 pending chkp writes
10 log i/o's done, 0.00 log i/o's/second
----------------------
BUFFER POOL AND MEMORY
----------------------
Total memory allocated 19546924; in additional pool allocated 1048576
Dictionary memory allocated 483888
Buffer pool size 512
Free buffers 381
Database pages 130
Modified db pages 0
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages read 863650, created 0, written 1
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
No buffer pool page gets since the last printout
--------------
ROW OPERATIONS
--------------
0 queries inside InnoDB, 0 queries in queue
1 read views open inside InnoDB
Main thread process no. 2096, id 2711718800, state: waiting for server activity
Number of rows inserted 0, updated 0, deleted 0, read 156489156
0.00 inserts/s, 0.00 updates/s, 0.00 deletes/s, 0.00 reads/s
----------------------------
END OF INNODB MONITOR OUTPUT
============================1 row in set (0.00 sec)
create procedure stu_pro ()
begin
(上述代码)end是否有影响
这么写对么?
然后看看这个系统统参数。max_write_lock_count 的值如果很大,看看 innodb_buffer_pool_size 的设置是多少。调大一些。
我将innodb_buffer_pool_size 设置为800M
这些还不够么?还有那个LIMIT不是只能用在select中么?
在修改和删除中好用么?
....
delete from yourTble order by id limit 1000;
UNTIL row_count()=0 END REPEAT;
ROW_COUNT()返回被前面语句升级的、插入的或删除的行数。 这个行数和 mysql 客户端显示的行数及 mysql_affected_rows() C API 函数返回的值相同。
mysql> INSERT INTO t VALUES(1),(2),(3);
问询完成, 表中有3 行 (0.00秒)
记录: 3 重复: 0 警告: 0
mysql> SELECT ROW_COUNT();
+-------------+
| ROW_COUNT() |
+-------------+
| 3 |
+-------------+
表中有1行 (0.00 秒)
mysql> DELETE FROM t WHERE i IN(1,2);
问询完成, 找到2 行 (0.00 秒)
mysql> SELECT ROW_COUNT();
+-------------+
| ROW_COUNT() |
+-------------+
| 2 |
+-------------+
表中有1行 (0.00 秒)
begin
repeat
update b2 inner join
(select name,max(id) as ma,min(id) as mi from b1 group by name having count(*)>=2) c
on b2.id=c.ma
set b2.id=c.mi
order bu b2.id limit 100;
until row_count()=0 end repeat;delete a from b1 a inner join b1 b
on a.name=b.name and a.id>b.id
end
这个存储过程错在哪里?
begin
repeat
update b2 inner join
(select name,max(id) as ma,min(id) as mi from b1 group by name having count(*)>=2) c
on b2.id=c.ma
set b2.id=c.mi
order bu b2.id limit 100;
until row_count()=0 end repeat; delete a from b1 a inner join b1 b
on a.name=b.name and a.id>b.id
end
这个存储过程错在哪里?
begin
repeat
update b2 inner join
(select name,max(id) as ma,min(id) as mi from b1 group by name having count(*)>=2) c
on b2.id=c.ma
set b2.id=c.mi
order bu b2.id limit 100;
until row_count()=0
end repeat;
delete a from b1 a inner join b1 b
on a.name=b.name and a.id>b.id
end
错误是:
ERROR 1064 (42000):you have an error in your SQL syntax;check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 6
ERROR 1064 (42000):you have an error in your SQL syntax;check the manual that corresponds to your MySQL server version for the right syntax to use near 'until cow_count()=0 end repeat' at line 1
ERROR 1064 (42000):you have an error in your SQL syntax;check the manual that corresponds to your MySQL server version for the right syntax to use near 'end' at line 3
ERROR 1064 (42000):you have an error in your SQL syntax;check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 6
ERROR 1064 (42000):you have an error in your SQL syntax;check the manual that corresponds to your MySQL server version for the right syntax to use near 'until cow_count()=0 end repeat' at line 1
ERROR 1064 (42000):you have an error in your SQL syntax;check the manual that corresponds to your MySQL server version for the right syntax to use near 'end' at line 3
先把语法错误处理了再试试。
drop procedure if exists MsgContent_pro;
create procedure MsgContent_pro ()
begin
repeat
update tbContent inner join (select msgContent,max(msgID) as ma,min(msgID) as mi from tbMsgContent group by msgContent having count(msgContent)>=2) c on tbContent.msgID=c.ma
set tbContent.msgID=c.mi
order by tbContent.msgID limit 100;
until row_count()=0 end repeat;
delete a from tbMsgContent a inner join tbMsgContent b
on a.msgContent=b.msgContent and a.msgID>b.msgID
end
错误是
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 6
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'until row_count()=0 end repeat' at line 1
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'end' at line 3
mysql>
Query OK, 0 rows affected, 1 warning (0.48 sec)mysql>
mysql> delimiter //
mysql>
mysql> create procedure MsgContent_pro ()
-> begin
-> repeat
-> update tbContent inner join
-> (select msgContent,max(msgID) as ma,min(msgID) as mi
-> from tbMsgContent
-> group by msgContent
-> having count(msgContent)>=2
-> ) c
-> on tbContent.msgID=c.ma
-> set tbContent.msgID=c.mi
-> order by tbContent.msgID limit 100;
-> until row_count()=0 end repeat;
-> delete a from tbMsgContent a inner join tbMsgContent b
-> on a.msgContent=b.msgContent and a.msgID>b.msgID;
-> end;
->
-> //
Query OK, 0 rows affected (0.08 sec)mysql>
mysql> delimiter ;
mysql>
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'until row_count()=0 end repeat' at line 1
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'end' at line 3
mysql> Aborted
mysql> source /home/guoling/桌面/xiangmu/procedure.sql
Query OK, 0 rows affected (0.00 sec)Query OK, 0 rows affected (0.00 sec)mysql> call MsgContent_pro//
ERROR 1221 (HY000): Incorrect usage of UPDATE and ORDER BY
mysql>
这个只是其中的一小部分
思想和我举得例子是一样的
调试是成功了
只是运行出现问题
这个问题是说update和order by的使用不正确吧?
create table tbMsgContent( msgID bigint not null, msgContent tinytext not null, primary key (msgID));
create table tbContent( reID bigint not null, msgTypeID integer not null, isRequest boolean not null, msgID bigint not null, textID bigint, fileID bigint);
mysql>
mysql> create procedure MsgContent_pro ()
-> begin
-> repeat
-> update tbContent
-> set msgID=( select mi from (select msgContent,max(msgID) as ma,min(msgID) as mi
-> from tbMsgContent
-> group by msgContent
-> having count(msgContent)>=2
-> ) c where ma=tbContent.msgID)
-> where msgID in (select ma from (select msgContent,max(msgID) as ma,min(msgID) as mi
-> from tbMsgContent
-> group by msgContent
-> having count(msgContent)>=2
-> ) c )
-> order by tbContent.msgID
-> limit 100;
-> until row_count()=0 end repeat;
-> delete a from tbMsgContent a inner join tbMsgContent b
-> on a.msgContent=b.msgContent and a.msgID>b.msgID;
-> end;
->
-> //
Query OK, 0 rows affected (0.01 sec)mysql>
mysql> delimiter ;
mysql> call MsgContent_pro();
Query OK, 0 rows affected (0.06 sec)mysql>
ERROR 1206 (HY000): The total number of locks exceeds the lock table size
mysql>