如何使用like运算符编写联接查询

时间:2019-05-08 06:40:58

标签: mysql laravel-5

我正在尝试在表中查找重复的(匹配名称)记录。

-----------------------------------
|id | name                        |
-----------------------------------
| 1 | Bielefeld Area, Germany     |
-----------------------------------
| 2 | Biella Area, Italy          | 
-----------------------------------
| 3 | Bilbao Area, Italy          | 
-----------------------------------
| 4 | Birmingham, United Kingdom  | 
-----------------------------------
| 5 | Blackburn, United Kingdom   | 
-----------------------------------
| 6 | Blackpool, United Kingdom   | 
-----------------------------------
| 7 | Bogotá D.C. Area, Colombia  |
-----------------------------------

我想过滤重复记录,例如伯明翰英国,布莱克本英国,意大利毕尔巴鄂地区

预期结果

-----------------------------------
|id | name                        |
-----------------------------------
| 2 | Biella Area, Italy          | 
-----------------------------------
| 3 | Bilbao Area, Italy          | 
-----------------------------------
| 4 | Birmingham, United Kingdom  | 
-----------------------------------
| 5 | Blackburn, United Kingdom   | 
-----------------------------------
| 6 | Blackpool, United Kingdom   | 
-----------------------------------

我已经尝试过下面的查询,但是没有用。

select p.* 
from countries p
left join countries u
on concat('|', p.name, '|') like concat('%|', u.name, '|%')

1 个答案:

答案 0 :(得分:0)

您可以在此处使用group by

Select count(id) as count, name from countries group by countries.name