sql字符串无法正确

时间:2018-06-13 13:49:04

标签: mysql sql

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

有点像这样

groups_idgroups_namegroups_descriptiongroups_activegroups_hashgroups_entry_dateuser_idgroups_emailgroups_sms

CUSTOMERS_GROUPS

customers_hashgroups_hash

客户

customers_idcustomers_first_namecustomers_surnamecustomers_telephonecustomers_emailcustomers_telephone_activecustomers_email_activeclient_typecustomers_hashcustomers_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

但是它让我回归零......

2 个答案:

答案 0 :(得分:0)

问题是你的where子句。它必须是on子句的一部分:

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 ) AND groups.groups_active ='1' 
GROUP BY customers.customers_entry_date

答案 1 :(得分:0)

问题在于:

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

应该是

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

(虽然目前还不清楚哈希实际代表什么,以及为什么没有桥表连接表格的ID。我在评论部分向你的请求提出了这个问题。)