eqxu的专栏

天使联盟

mysql 一些常见应用 concat/@a/case/limit/

 

use test;
create table tt
(f1 varchar(30), f2 int );

INSERT  tt values('a',1);
INSERT  tt values('a',1);
INSERT  tt values('a',2);
INSERT  tt values('a',1);
INSERT  tt values('b',1);
INSERT  tt values('b',1);
INSERT  tt values('b',1);

mysql> select * from tt;
+------+------+
| f1   | f2   |
+------+------+
| a    |    1 |
| a    |    1 |
| a    |    2 |
| a    |    1 |
| b    |    1 |
| b    |    1 |
| b    |    1 |
+------+------+
7 rows in set (0.00 sec)

mysql> select @a:=f1,@b:=f1 from tt order by f1 limit 1;
+--------+--------+
| @a:=f1 | @b:=f1 |
+--------+--------+
| a      | a      |
+--------+--------+
1 row in set (0.00 sec)

mysql> select case flag when 0 then '' else concat('total ',@b,':') end,@b:=f1,f1,f2 from (select f1,f2,(case f1 when @a then 0 else 1 end) flag ,@a:=f1 from tt order by f1 ) tb;
+-----------------------------------------------------------+--------+------+------+
| case flag when 0 then '' else concat('total ',@b,':') end | @b:=f1 | f1   | f2   |
+-----------------------------------------------------------+--------+------+------+
|                                                           | a      | a    |    1 |
|                                                           | a      | a    |    1 |
|                                                           | a      | a    |    2 |
|                                                           | a      | a    |    1 |
| total a:                                                  | b      | b    |    1 |
|                                                           | b      | b    |    1 |
|                                                           | b      | b    |    1 |
+-----------------------------------------------------------+--------+------+------+
7 rows in set (0.00 sec)

阅读更多
文章标签: mysql insert table
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭