MySQL 變數 、rownum 、 一行拆多行..小知識點
變數:
mysql> select @host:=host from mysql.user where host!='localhost';
+----------------+
| @host:=host |
+----------------+
| 127.0.0.1 | --> set @host:='127.0.0.1';
| ::1 | --> set @host:='::1';
+----------------+
| @host:=host |
+----------------+
| 127.0.0.1 | --> set @host:='127.0.0.1';
| ::1 | --> set @host:='::1';
| wonhigh-test16 | --> set @host:='wonhigh-test16';
+----------------+
mysql> select @host; ---查詢時只會取最後一行....不會是自己想的3行都顯示;
+---------------+
| @host |
+----------------+
| wonhigh-test16 |
+----------------+
mysql>
mysql> select @host; ---查詢時只會取最後一行....不會是自己想的3行都顯示;
+---------------+
| @host |
+----------------+
| wonhigh-test16 |
+----------------+
mysql>
rownum:
mysql> select host,@rownum:=@rownum+1 AS rownum from mysql.user,(SELECT @rownum:=0) r where host!='localhost';
+-----------------+--------------+
| host | rownum |
+-----------------+--------------+
+-----------------+--------------+
| host | rownum |
+-----------------+--------------+
| 127.0.0.1 | 1 |
| ::1 | 2 |
| wonhigh-test16 | 3 |
+------------------+------------+
mysql>
或者
set @rownum:=0;
select host,@rownum:=@rownum+1 AS rownum from mysql.user where host!='localhost';
select host,@rownum:=@rownum+1 AS rownum from mysql.user where host!='localhost';
一行拆多行:
mysql>select @host:=GROUP_CONCAT(host) host1 from mysql.user where host!='localhost';
+---------------------------------+
| host1 |
+--------------------------------+
| 127.0.0.1,::1,wonhigh-test16 |
+---------------------------------+
+---------------------------------+
| host1 |
+--------------------------------+
| 127.0.0.1,::1,wonhigh-test16 |
+---------------------------------+
mysql>
mysql> SELECT substring_index(substring_index(a.host1, ',', b.id),',' ,-1 ) as host1_,b.id
FROM (select @host:=GROUP_CONCAT(host) host1 from mysql.user where host!='localhost') a
JOIN (select 1 id union all select 2 union all select 3) b
FROM (select @host:=GROUP_CONCAT(host) host1 from mysql.user where host!='localhost') a
JOIN (select 1 id union all select 2 union all select 3) b
ON b.id<= (length(a.host1) - length(REPLACE(a.host1, ',', '')) + 1) order by b.id;
+-------------------+---------------+
| host1_ | id |
+------------------+-----------------+
| 127.0.0.1 | 1 |
| ::1 | 2 |
| wonhigh-test16 | 3 |
+-----------------+----------------+
mysql>
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28602568/viewspace-2092790/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 浮點數小知識點
- MySQL知識點MySql
- java小知識點Java
- js小知識點JS
- 小知識點1
- 前端小知識點前端
- mongo 小知識點Go
- javascript小知識點JavaScript
- PHP小知識點PHP
- Mysql 索引知識點MySql索引
- 【JAVA】- 知識點小結Java
- promise知識點小結Promise
- CSS小知識點一CSS
- HTTP知識點小結HTTP
- makefile 知識點小結
- ElasticSearch知識點小記Elasticsearch
- VEEAM的小知識點
- MySQL 變數及效能狀態檢視知識技巧MySql變數
- MySQL知識點總結MySql
- 一起認識下,那些我們應該知道的mysql中的變數知識點MySql變數
- javascript中關於value的一個小知識點(value既是屬性也是變數)JavaScript變數
- mysql小記--基礎知識MySql
- 前端小知識點彙總前端
- 前端(js html)小知識點前端JSHTML
- 記錄的小知識點
- node知識點小結(一)
- golang小知識點記錄Golang
- iOS小知識點羅列iOS
- shell知識點小結2
- awk知識點小結1
- shell知識點小結1
- MySQL MVCC知識點彙總MySqlMVC
- MySQL 索引知識點總結MySql索引
- MySQL 叢集知識點整理MySql
- mysql 常用知識點總結MySql
- 面試小知識:MySQL索引相關面試MySql索引
- MySQL基礎知識小結(一)MySql
- Oracle多行轉一行Oracle