sql字符串不能正确

ddarikpa  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(338)

我想从我的数据库里得到一些数据。
有点像这样
groups_id , groups_name , groups_description , groups_active , groups_hash , groups_entry_date , user_id , groups_email , groups_sms 客户群 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 我想要customers.groups\u散列和groups.groups\u名称在concat表单中。这是我的尝试。。。

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

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

b4wnujal

b4wnujal1#

问题在于:

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

应该是

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

(虽然还不清楚散列实际上代表什么,以及为什么没有桥接表来连接表的id。我在你的请求的评论部分问了这个问题。)

k5ifujac

k5ifujac2#

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

  1. SELECT * , GROUP_CONCAT( DISTINCT customers_groups.groups_hash
  2. SEPARATOR '/' ) , GROUP_CONCAT( groups.groups_name
  3. SEPARATOR '/' )
  4. FROM customers
  5. INNER JOIN customers_groups ON ( customers.customers_hash = customers_groups.customers_hash )
  6. LEFT JOIN groups ON ( customers_groups.customers_hash = groups.groups_hash ) AND groups.groups_active ='1'
  7. GROUP BY customers.customers_entry_date

相关问题