Строка SQL не может получить право - PullRequest
0 голосов
/ 13 июня 2018

Я пытаюсь получить некоторые данные из моей базы данных.

Это выглядит примерно так

ГРУППЫ

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

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

Я хочу customer.groups_hash и groups.groups_name в форме concat,Вот моя попытка ...

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 голосов
/ 14 июня 2018

Проблема здесь:

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

, которая, вероятно, должна быть

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

(Хотя пока неясно, что на самом деле представляют хеши и почему нет таблицы мостов, связывающей таблицы«Вместо идентификаторов. Я задал этот вопрос в разделе комментариев к вашему запросу.)

0 голосов
/ 13 июня 2018

Проблема в вашем предложении 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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...