按名称选择DISTINCT,但从另一个表

时间:2017-10-03 00:04:30

标签: mysql

-

我有以下MySQL查询:

-

SELECT CONCAT(COALESCE(customer.last_name,''), ' ', COALESCE(customer.first_name,'')) 
as fullName, 
customer.first_name, customer.last_name, customer.email, 
address.phone, customer.customer_id 

FROM repairs

LEFT JOIN customer
ON customer.customer_id = repairs.customer_id

LEFT JOIN address
ON address.address_id = customer.address_id

WHERE CONCAT(COALESCE(customer.first_name,''), ' ', COALESCE(customer.last_name,'')) LIKE :search 
OR CONCAT(COALESCE(customer.last_name,''), ' ', COALESCE(customer.first_name,'')) LIKE :search 
OR customer.business_name LIKE :search OR customer.email LIKE :search OR address.phone LIKE :search

GROUP BY fullName

ORDER BY fullName ASC

LIMIT 15

这里还有一些数据:

客户表)

customer_id | last_name | first_name   |    email           | address_id
1              Vandalay    Art            van@gmail.com           2
2              Vandalay    Art            van123y@yahoo.com       3
3              Dude        George         george79@gmail.com      4 
4              Christian   Forge          chrfrg@gmail.com        5

地址表格)

address_id | address                 | phone
2            1st Art Institute, 23    +44392031
3            24 Oak Street, Berkl.    +62342354
4            51st Main, California    +662341232 

维修表格)

repair_id| customer_id |   other fields
   1             1         ...
   2             1         ...
   3             1         ...
   4             2         ...
   5             2         ...
   6             1         ...
   7             3         ...
   8             3         ...
   9             2         ...

我使用的表格是维修客户地址

我在 customer.last_name customer.first_name 上使用CONCAT生成我正在搜索的全名和COALESCE,因为有时其中一个是NULL。

如果恰好有2个人使用相同的姓名,但不同的地址和电子邮件,我的查询将仅显示其中一个,因为我的群组在fullName上,并指向只是两个人中的一个(以及 client_id )。

我需要回复我的查询 DISTINCT 名称,但当地址/电子邮件对于同名的2人不同时,我需要包含两个人(显示2个不同的地址) 。所以我猜没有Group BY ......而是使用DISTINCT。

希望输出样本:

1              Vandalay    Art            van@gmail.com         
2              Vandalay    Art            van123y@yahoo.com      
3              Dude        George         george79@gmail.com      
4              Christian   Forge          chrfrg@gmail.com

..如此独特的名字,但如果人们碰巧有相同的名字,我需要重复,但实际上是2个不同的人"

到目前为止,我只获得了一个艺术万代莱,因为我是BY全名......

感谢任何帮助。

0 个答案:

没有答案
相关问题