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

编程入门 行业动态 更新时间:2024-10-11 17:25:43
本文介绍了如何在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

所有帐户不是禁用,并且显示他们的权限我根据情景获取了所有帐户但问题是如果一个帐户有多个产品,那么它显然显示帐户ID超过一​​次

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

* 我在寻找 GROUP BY 在 HAVING 子句之后检查权限的 a.id * 但没有运气得到语法错误

*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

这是我的查询

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

更多推荐

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

本文发布于:2023-11-22 01:42:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1615484.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:子句   如何在   GROUP   Mysql

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!