获取 MySQL 中的列子字符串
使用 SUBSTR() 方法获取列的子字符串。
我们首先创建一个表 -
mysql> create table DemoTable -> ( -> Title text -> ); Query OK, 0 rows affected (0.74 sec)
使用 insert 命令在表中插入一些记录 -
mysql> insert into DemoTable values('This is a MySQL Database'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('Java is an Object Oriented Programming Language'); Query OK, 1 row affected (0.17 sec)
使用 select 语句显示表中的所有记录 -
mysql> select *from DemoTable ;
输出
这将生成以下输出 -
+-------------------------------------------------+ | Title | +-------------------------------------------------+ | This is a MySQL Database | | Java is an Object Oriented Programming Language | +-------------------------------------------------+ 2 rows in set (0.00 sec)
以下是可以选择列子字符串的查询 -
mysql> SELECT distinct(substr(Title, 1, 8)) from DemoTable;
输出
这将生成以下输出 -
+-----------------------+ | (substr(Title, 1, 8)) | +-----------------------+ | This is | | Java is | +-----------------------+ 2 rows in set (0.00 sec)
广告