这是获取特定用户帐户的查询,产品与帐户相关,one-to-many
因此每个产品有四个权限
所有不是的帐户,Disable
也显示他们的权限我根据场景获得了所有帐户,但问题是如果一个帐户有多个产品,那么它显然不止一次显示帐户 ID
*我在检查权限的子句后寻找什么GROUP BY
*但a.id
没有HAVING
运气得到语法错误
Error Code: 1064 right syntax to use near 'GROUP BY a.`id` LIMIT 0, 1000' at line 14
这是我的查询
SELECT a.`id` AS aid,ap.`pid`, p.`products_name`, a.accounts_account_number, c.clients_name,a.accounts_product_type AS pptype,
c.`clients_last_name`, a.`accounts_account_name`
,(SELECT `status` FROM `bf_account_permissions` WHERE `permission`='Readonly' AND uid=2 AND aid=a.id AND pid=ap.`pid`) AS Readonly,
(SELECT `status` FROM `bf_account_permissions` WHERE `permission`='Buy' AND uid=2 AND aid=a.id AND pid=ap.`pid`) AS Buy,
(SELECT `status` FROM `bf_account_permissions` WHERE `permission`='Sell' AND uid=2 AND aid=a.id AND pid=ap.`pid`) AS Sell,
(SELECT `status` FROM `bf_account_permissions` WHERE `permission`='Disable' AND uid=2 AND aid=a.id AND pid=ap.`pid`) AS `Disable`
FROM `bf_clients` c INNER JOIN `bf_user_clients` uc ON (c.`id` = uc.`client_id`)
INNER JOIN `bf_accounts` a ON (c.`id` = a.`accounts_clients`)
INNER JOIN `bf_account_products` ap ON (a.`id` = ap.`aid`)
INNER JOIN `bf_products` p ON (ap.`pid` = p.`id`)
WHERE uc.`agt_user_id`=2 AND a.deleted=0 AND c.deleted=0
HAVING (Readonly !='' OR Buy !='' OR Sell !='' OR `Disable` !='' )
AND `Disable` !='1' ORDER BY a.`id` GROUP BY a.`id`
任何帮助,将不胜感激