在 MySQL Select 中兩次顯示列值?


您可以使用 concat()。讓我們首先建立一個表 -

mysql> create table DemoTable
(
   Value int
);
Query OK, 0 rows affected (0.73 sec)

使用插入命令插入一些記錄到表中 -

mysql> insert into DemoTable values(100);
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable values(200);
Query OK, 1 row affected (0.08 sec)
mysql> insert into DemoTable values(100);
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable values(200);
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable values(200);
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable values(100);
Query OK, 1 row affected (0.30 sec)

使用 select 語句從表中顯示所有記錄 -

mysql> select *from DemoTable;

這將生成以下輸出 -

+-------+
| Value |
+-------+
|   100 |
|   200 |
|   100 |
|   200 |
|   200 |
|   100 |
+-------+
6 rows in set (0.00 sec)

以下是 MySQL 中兩次顯示列值查詢 -

mysql> select concat(Value,'~',Value) from DemoTable group by Value;

這將生成以下輸出 -

+-------------------------+
| concat(Value,'~',Value) |
+-------------------------+
|                 100~100 |
|                 200~200 |
+-------------------------+
2 rows in set (0.00 sec)

更新於:2019-09-30

230 次瀏覽

開啟你的 事業

完成課程,獲得認證

開始學習
廣告
© . All rights reserved.