使用一条 MySQL 查询显示不同长度的子字符串并合并结果
我们先创建一个表 -
mysql> create table DemoTable856(Title text); Query OK, 0 rows affected (0.89 sec)
使用 insert 命令在表中插入一些记录 -
mysql> insert into DemoTable856 values('Introduction to MySQL'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable856 values('Java in depth'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable856 values('C++ with Data Structure'); Query OK, 1 row affected (0.17 sec)
select 语句从表中显示所有记录 -
mysql> select *from DemoTable856;
这将生成以下输出 -
+-------------------------+ | Title | +-------------------------+ | Introduction to MySQL | | Java in depth | | C++ with Data Structure | +-------------------------+ 3 rows in set (0.00 sec)
以下是使用 UNION ALL 显示不同长度的子字符串并合并结果的查询 -
mysql> select substr(Title,1,12) from DemoTable856 UNION ALL select substr(Title,1,4) from DemoTable856;
这将生成以下输出 -
+--------------------+ | substr(Title,1,12) | +--------------------+ | Introduction | | Java in dept | | C++ with Dat | | Intr | | Java | | C++ | +--------------------+ 6 rows in set (0.03 sec)
广告