Mysql不同的组由

时间:2013-04-01 21:22:13

标签: mysql group-by distinct

快乐的帕斯卡日。

('Afghanistan - Fixed'),
('Afghanistan - Mobile AWCC'),
('Afghanistan - Mobile Etisalat'),
('Afghanistan - Mobile MTN'),
('Afghanistan - Mobile Others'),
('Afghanistan - Mobile Roshan'),
('Afghanistan - Mobile WASEL'),
('Albania - Fixed'),
('Albania - Fixed ALBTEL'),
('Albania - Fixed AMC'),
('Albania - Fixed Tirana'),
('Albania - Interactive TV'),
('Albania - Mobile'),
('Albania - Mobile AMC'),
('Albania - Mobile Plus'),
('Albania - Mobile Vodafone'),
('Algeria - Fixed'),
('Algeria - Fixed CAT'),
('Algeria - Mobile Orascom'),
('Algeria - Mobile Others'), ...

我需要让阿尔及利亚,阿尔巴尼亚和阿富汗与众不同吗?你知道吗?

2 个答案:

答案 0 :(得分:1)

如果我理解正确你可能需要这样的东西

SELECT DISTINCT TRIM(SUBSTRING_INDEX(field1, '-', 1)) countries
FROM table1

输出:

|   COUNTRIES |
---------------
| Afghanistan |
|     Albania |
|     Algeria |

<强> SQLFiddle example

这也可行

SELECT TRIM(SUBSTRING_INDEX(field1, '-', 1)) countries
FROM table1
GROUP BY TRIM(SUBSTRING_INDEX(field1, '-', 1))

<强> SQLFiddle example

答案 1 :(得分:0)

select distinct SUBSTRING_INDEX(text, ' - ', 1) from test;

http://www.sqlfiddle.com/#!2/ba297/3