SELECT CO.CUST_NO,CO.CONTACT_NAME, CO.PHONE,ROW_NUMBER() OVER(PARTITION BY CO.CUST_NO ORDER BY ROWNUM) AS CU FROM ERMAS_CONTACT CO
oracle 语句转为MySQL 求高手们帮帮忙
oracle 语句转为MySQL 求高手们帮帮忙
解决方案
15
--mssql 区,本人给你回复了 -- 给你写个例子,rownum 是你想要的结果,你本人研究一下; mysql> mysql> create table test(id int , name varchar(10)) ; Query OK, 0 rows affected (0.01 sec) mysql> insert into test(id, name) values(1,"qq"); Query OK, 1 row affected (0.00 sec) mysql> insert into test(id, name) values(1,"aa"); Query OK, 1 row affected (0.00 sec) mysql> insert into test(id, name) values(1,"zz"); Query OK, 1 row affected (0.00 sec) mysql> insert into test(id, name) values(2,"tt"); Query OK, 1 row affected (0.00 sec) mysql> insert into test(id, name) values(2,"yy"); Query OK, 1 row affected (0.00 sec) mysql> mysql> set @x=0; Query OK, 0 rows affected (0.00 sec) mysql> set @id=0; Query OK, 0 rows affected (0.00 sec) mysql> select @x:= if(@id= id ,@x , 0) +1 as rownum, @id := id as previd, t.* -> from test t ; +--+--+--+--+ | rownum | previd | id | name | +--+--+--+--+ | 1 | 1 | 1 | qq | | 2 | 1 | 1 | aa | | 3 | 1 | 1 | zz | | 1 | 2 | 2 | tt | | 2 | 2 | 2 | yy | +--+--+--+--+ 5 rows in set (0.00 sec) mysql> mysql> drop table test ; Query OK, 0 rows affected (0.00 sec) mysql>
5
借宝贴回复一下。
create table test(id int , name varchar(10)) ; insert into test(id, name) values(1,"qq"); insert into test(id, name) values(1,"aa"); insert into test(id, name) values(1,"zz"); insert into test(id, name) values(2,"tt"); insert into test(id, name) values(2,"yy"); select @x:= if(@id= id ,@x , 0) +1 as rownum_partitioned_by_id, @id := id as previd, t.* from test t, (select @x:= 0, @id := 0) d order by id;
许多有连接池的应用程序里,写多个sql和环境影响都比较麻烦。
所以把变量初始化写在同一个sql里就更方便了。