我用的是MySql,我有这样的表

id |city               | address         | work_time
-------------------------------
1  |London - center    |London street 1  |7am - 10 pm
2  |London - center 2  |London street 2  |7am - 10 pm
3  |London - center 3  |London street 3  |7am - 10 pm
4  |Paris - bol 1      |Bol street 3     |9am - 10 pm
5  |Paris - bol 2      |Bol street 13    |9am - 10 pm

如何按-前的第一个字符串对所有城市进行分组,并裁剪此符号后的所有字母?
我只想在名单上显示伦敦和巴黎
  id |city
  -------------------------------
  1  |London
  2  |Paris

最佳答案

您可以使用SUBSTRING_INDEXSELECT DISTINCT

SELECT DISTINCT SUBSTRING_INDEX(city, ' -', 1) AS city
FROM yourtable

Demo on dbfiddle

关于mysql - SQL按字符串分组并按符号切割结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54744388/

10-11 03:17