为了获得包括空格的独特性,您可以使用以下语法-
SELECT DISTINCT replace(yourColumnName,' ','') FROM yourTableName;
让我们首先创建一个表:
mysql>create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Name varchar(20) );
使用插入命令在表中插入一些记录-
mysql>insert into DemoTable(Name) values('John '); mysql>insert into DemoTable(Name) values(' John '); mysql>insert into DemoTable(Name) values('John'); mysql>insert into DemoTable(Name) values('Sam'); mysql>insert into DemoTable(Name) values('Carol'); mysql>insert into DemoTable(Name) values(' Sam'); mysql>insert into DemoTable(Name) values('Mike '); mysql>insert into DemoTable(Name) values('David');
以下是使用select语句显示表中所有记录的查询:
mysql>select *from DemoTable;
这将产生以下输出-
+----+-----------+ | Id | Name | +----+-----------+ | 1 | John | | 2 | John | | 3 | John | | 4 | Sam | | 5 | Carol | | 6 | Sam | | 7 | Mike | | 8 | David | +----+-----------+ 8 rows in set (0.00 sec)
这是获取包含空格的查询:
mysql>SELECT DISTINCT replace(Name,' ','') FROM DemoTable;
这将产生以下输出-
+----------------------+ | replace(Name,' ','') | +----------------------+ | John | | Sam | | Carol | | Mike | | David | +----------------------+ 5 rows in set (0.00 sec)