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
- js小知識點JS
- mongo 小知識點Go
- 前端小知識點前端
- java小知識點Java
- 小知識點1
- MySQL 變數及效能狀態檢視知識技巧MySql變數
- MySQL知識點總結MySql
- 一起認識下,那些我們應該知道的mysql中的變數知識點MySql變數
- 【JAVA】- 知識點小結Java
- promise知識點小結Promise
- VEEAM的小知識點
- ElasticSearch知識點小記Elasticsearch
- makefile 知識點小結
- MySQL MVCC知識點彙總MySqlMVC
- mysql 常用知識點總結MySql
- MySQL 叢集知識點整理MySql
- MySQL 索引知識點總結MySql索引
- 前端(js html)小知識點前端JSHTML
- 前端小知識點彙總前端
- 記錄的小知識點
- Python小知識點隨筆Python
- 前端小知識10點(2019.5.2)前端
- 前端小知識10點(2019.4.14)前端
- node知識點小結(一)
- MySQL基礎知識小結(一)MySql
- mysql的一些知識點MySql
- 總結 MySQL 相關知識點MySql
- mysql併發控制原理知識點MySql
- 總結的小知識點(一)
- Laravel 小知識點之 HtmlString 類LaravelHTML
- 雜湊表知識點小結
- 2021/06/02知識點小結
- Js基礎知識(一) – 變數JS變數
- 面試小知識:MySQL索引相關面試MySql索引
- 深入理解mysql-進階知識點,啟動項、系統變數、字符集介紹!MySql變數
- 小知識|Java中的"魔數"Java
- MySQL 常用易混淆知識點總結MySql