2

这是获取特定用户帐户的查询,产品与帐户相关,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`

任何帮助,将不胜感激

4

3 回答 3

4

您可以尝试使用外部选择

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
于 2013-06-19T08:55:48.967 回答
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
于 2013-06-19T10:00:38.633 回答
1

HAVING应该在 A 之后使用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'
于 2013-06-19T08:48:51.577 回答