通过单个MySQL查询显示不同长度的子字符串,并将结果合并
让我们首先创建一个表-
mysql> create table DemoTable856(Title text);
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable856 values('Introduction to MySQL'); mysql> insert into DemoTable856 values('Java in depth'); mysql> insert into DemoTable856 values('C++ with Data Structure');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable856;
这将产生以下输出-
+-------------------------+ | Title | +-------------------------+ | Introduction to MySQL | | Java in depth | | C++ with Data Structure | +-------------------------+ 3 rows in set (0.00 sec)
以下是显示不同长度的子字符串并使用UNIONALL组合结果的查询-
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)