Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql Having子句之后如何使用GROUPBY_Mysql_Database_Join_Group By_Having - Fatal编程技术网

Mysql Having子句之后如何使用GROUPBY

Mysql Having子句之后如何使用GROUPBY,mysql,database,join,group-by,having,Mysql,Database,Join,Group By,Having,这是一个查询,用于获取特定用户的帐户产品与一对多关系中的帐户相关,因此每个产品有四个权限 所有未禁用的帐户都将禁用,并显示其权限 我根据场景获得了所有帐户,但问题是如果一个帐户有多个产品,那么它显然会多次显示帐户id *在检查权限*但没有语法错误的HAVING子句之后,通过a.id对进行分组 Error Code: 1064 right syntax to use near 'GROUP BY a.`id` LIMIT 0, 1000' at line 14 这是我的问题 SELECT a.

这是一个查询,用于获取特定用户的帐户产品与一对多关系中的帐户相关,因此每个产品有四个权限

所有未禁用
的帐户都将禁用
,并显示其权限
我根据场景获得了所有帐户,但问题是如果一个帐户有多个产品,那么它显然会多次显示帐户id

*在检查权限*但没有语法错误的
HAVING
子句之后,
通过
a.id
进行分组

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`

任何帮助都将不胜感激

拥有的
应在
分组后使用

试试这个:

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'

您可以尝试使用外部选择

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

您有相关的子查询,这些子查询通常执行得很差。可以重写以使用联接,这也使得您可以检查WHERE子句中的值

不确定在没有聚合函数的情况下为什么要使用GROUP BY

类似这样的东西(未经测试)


我想检查
GROUP BY
之前的所有条件,然后使用where子句中的子句,而不是HAVING中的子句,因为HAVING的目的是过滤组中的选择如何检查
where
中的别名,
alias
通过
have
检查,您通常使用have来检查聚合函数的结果,而不是别名字段。问题是在主选择中有子选择,这可能很慢,需要在选择中再次重复以检查结果。另外,当您不聚合任何字段时,为什么要使用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