mysql - 如何使用 like 运算符编写连接查询

标签 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, '|%')

最佳答案

您可以在此处使用group by:

选择 count(id) 作为计数,按国家/地区分组的名称。名称

关于mysql - 如何使用 like 运算符编写连接查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56034918/

相关文章:

laravel - 如何在 Laravel TestCase 中设置全局 header ?

php - Laravel 5.1 - 使用路由组时出现重复的前缀

MySQL 日期时间案例

java - 为什么 hibernate 在最后添加到我的查询交叉连接

php - Laravel:具有关系的查询构建器

php - 使用 PHP 和 MySQL 创建的框架的 URL 问题

Laravel 在 Controller 内部使用 Excel 包

php - Laravel SQL 查询返回错误结果

php - 将多个 where 子句与 laravel 查询构建器一起使用

php - 如何在 SQL (MySQL) 中合并两个表?