having子句的使用mysql,如何在having子句Mysql之后使用GROUP BY

Here is the query which gets the accounts for a specific user the products are related to accounts in one-to-many relation so for each product there are four permissions

All the accounts which is not Disable and also shows their permissions

I got all accounts according to scenario but the problem is if one account has more than one products then it obviously shows account id more than one time

*What i am looking for to GROUP BY the a.id after the HAVING clause which checks the permissions * but no luck getting syntax error

Error Code: 1064 right syntax to use near 'GROUP BY a.`id` LIMIT 0, 1000' at line 14

Here is my query

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`

Any help would be appreciated

解决方案

You can try to use an outer select

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值