首页 > 解决方案 > sql字符串无法正确

问题描述

我正在尝试从我的数据库中获取一些数据。

有点像这样

groups_id, groups_name, groups_description, groups_active, groups_hash, groups_entry_date, user_id, groups_email,groups_sms

CUSTOMERS_GROUPS

customers_hash,groups_hash

顾客

customers_id, customers_first_name, customers_surname, customers_telephone, customers_email, customers_telephone_active, customers_email_active, client_type, customers_hash,customers_entry_date

我想要一个 concat 形式的 customers.groups_hash 和 groups.groups_name。这是我的尝试...

SELECT * , GROUP_CONCAT( DISTINCT customers_groups.groups_hash
SEPARATOR '/' ) , GROUP_CONCAT( groups.groups_name
SEPARATOR '/' )
FROM customers
INNER JOIN customers_groups ON ( customers.customers_hash = customers_groups.customers_hash )
LEFT JOIN groups ON ( customers_groups.customers_hash = groups.groups_hash )
WHERE groups.groups_active ='1' GROUP BY customers.customers_entry_date

但它给了我一个零集......

标签: mysqlsql

解决方案


问题在这里:

LEFT JOIN groups ON ( customers_groups.customers_hash = groups.groups_hash )

这可能应该是

LEFT JOIN groups ON ( customers_groups.groups_hash = groups.groups_hash )

(虽然尚不清楚哈希实际代表什么以及为什么没有桥接表链接表的 ID。我已在评论部分根据您的要求提出了这个问题。)


推荐阅读