我正在尝试从我的数据库中获取一些数据。
有点像这样
组
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
但是它让我回归零......
答案 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。我在评论部分向你的请求提出了这个问题。)