以下是获取特定用户的帐户的查询,该帐户的产品与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`
任何帮助将不胜感激
答案 0 :(得分:3)
您可以尝试使用外部选择
SELECT q.*
FROM
(
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'
) q
GROUP BY aid
答案 1 :(得分:1)
HAVING
应在GROUP BY
试试这个::
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
ORDER BY a.`id` GROUP BY a.`id` HAVING (Readonly !='' OR Buy !='' OR Sell !='' OR `Disable` !='' )
AND `Disable` !='1'
答案 2 :(得分:1)
您的核心子查询通常表现不佳。可以重写以使用连接,这也很明显可以检查WHERE子句中的值。
当您没有聚合函数时,不确定为什么使用GROUP BY。
像这样(未经测试)
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`,
ReadonlySub.status AS Readonly,
BuySub.status AS Buy,
SellSub.status AS Sell,
DisableSub.status 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)
LEFT OUTER JOIN(SELECT `status` FROM bf_account_permissions WHERE `permission`='Readonly') ReadonlySub
ON ReadonlySub.uid = uc.agt_user_id AND ReadonlySub.aid = a.id AND ReadonlySub.pid = ap.pid
LEFT OUTER JOIN(SELECT `status` FROM bf_account_permissions WHERE `permission`='Buy') BuySub
ON BuySub.uid = uc.agt_user_id AND BuySub.aid = a.id AND BuySub.pid = ap.pid
LEFT OUTER JOIN(SELECT `status` FROM bf_account_permissions WHERE `permission`='Sell') SellSub
ON SellSub.uid = uc.agt_user_id AND SellSub.aid = a.id AND SellSub.pid = ap.pid
LEFT OUTER JOIN(SELECT `status` FROM bf_account_permissions WHERE `permission`='Disable') DisableSub
ON DisableSub.uid = uc.agt_user_id AND DisableSub.aid = a.id AND DisableSub.pid = ap.pid
WHERE uc.agt_user_id=2
AND a.deleted=0
AND c.deleted=0
AND (ReadonlySub.status !=''
OR BuySub.status !=''
OR SellSub.status !=''
OR DisableSub.status !='' )
AND DisableSub.status !='1'
ORDER BY a.id