mysql中的多个连接,有3个表

abithluo  于 2021-07-29  发布在  Java
关注(0)|答案(2)|浏览(346)

我有三张table

branch-
  br_id,
  br_name

users 
 user_id
 user_name
 user_type (2 types 'owner' 'admin')
 user_branch (associated branch )
 user_branches (comma seperated branch ids in case of 'admin' type )

item 
  it_id
  it_amount
  it_owner
  it_admin 
  it_activated

在这里,每个项目都属于用户类型“owner”
每个项目还由用户类型“admin”关联
我想要的是分支和相关的总金额和总计数列表


**Branch**         **total****total amount**

some branch Name    5   500
another Branch Name 7   780

如何在单个查询中完成
我试过这个,它显示了所有分支的数量

SELECT br_name,
count(it_id),
SUM(it_amount),
FROM branch
LEFT JOIN users ON FIND_IN_SET(br_id,user_branches)>0
LEFT JOIN item ON it_admin=ad_id
WHERE it_activated=1
GROUP BY br_name

但我在所有的分支中都得到了相同的计数和数量

7xllpg7q

7xllpg7q1#

我不知道你想从查询中得到什么,所以我猜了一下。我的答案查询首先按所有者,然后按管理员查找链接到每个分支的项目的总计数和总金额。
在查询中,有一个字段“ad\u id”:

LEFT JOIN item ON it_admin=ad_id

我想应该是“用户id”?
我还注意到,在您的查询中,您根本没有引用所有者。如果您实际上是在查找仅由管理员链接到分支的项的结果,则可以删除/忽略以下查询中的前两个子查询:

SELECT   br_name as branchname,
            (SELECT COUNT( DISTINCT it_id) 
                FROM item i1 
                JOIN users u1 ON i1.it_owner = u1.user_id AND i1.it_activated=1
                WHERE u1.user_branch = b.br_id AND u1.user_type = "owner") as ownertotal,
            (SELECT COUNT( DISTINCT it_id) 
                FROM item i2 
                JOIN users u2 ON i2.it_admin = u2.user_id AND i2.it_activated=1
                WHERE FIND_IN_SET(b.br_id, u2.user_branches) != 0 AND u2.user_type = "admin") as admintotal ,
            (SELECT SUM(i3.it_amount) 
                FROM item i3 
                JOIN users u3 ON i3.it_owner = u3.user_id AND i3.it_activated=1
                WHERE u3.user_branch = b.br_id AND u3.user_type = "owner") as owneramount,
            (SELECT SUM(i4.it_amount) 
                FROM item i4 
                JOIN users u4 ON i4.it_admin = u4.user_id AND i4.it_activated=1
                WHERE FIND_IN_SET(b.br_id, u4.user_branches) != 0 AND u4.user_type = "admin") as adminamount
    FROM branch b;
kfgdxczn

kfgdxczn2#

首先,你真的应该提供最低限度,特别是当要求与悬赏。不知道您的表结构和示例内容会使其他人更难提供帮助。我已经创建了示例create tables和insert,然后是查询。如果有不准确的地方,请纠正我。

CREATE TABLE branch (
  br_id int unsigned NOT NULL,
  br_name varchar(10) NOT NULL,
  PRIMARY KEY (br_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE users (
  user_id int unsigned NOT NULL,
  user_name varchar(10) NOT NULL,
  user_type varchar(10) NOT NULL,
  user_branch int NOT NULL,
  user_branches varchar(10) NOT NULL,
  PRIMARY KEY (user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE item (
  it_id int unsigned NOT NULL,
  it_amount int NOT NULL,
  it_owner int NOT NULL,
  it_admin int NOT NULL,
  it_activated int NOT NULL,
  PRIMARY KEY (it_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

insert into branch ( br_id, br_name )
  values 
  ( 1, 'b1' ), 
  ( 2, 'b2'), 
  ( 3, 'b3' );

insert into users ( user_id, user_name, user_type, user_branch, user_branches )
  values 
  ( 1, 'n1', 'owner', 1, '' ),
  ( 2, 'n2', 'admin', 2, '2, 3' ),
  ( 3, 'n3', 'owner', 3, '' );

insert into item ( it_id, it_amount, it_owner, it_admin, it_activated )
  values
  ( 1, 100, 1, 2, 1 ),
  ( 2, 150, 3, 2, 1 ),
  ( 3, 125, 1, 2, 1 ),
  ( 4, 30, 3, 2, 0 );

现在,我认为您需要的是一个联合查询,分别覆盖所有者和管理员,否则,您将得到全面的双重计数。第二,我认为当您使用逗号分隔的列表来保存多个id时,并不是表设计的最佳布局,例如您的管理员用户。但如果这就是你所拥有的,不能改变它,那就随它去吧。
那么,现在开始查询。分别看每一个。如果按用户的所有者类型进行查询,则只在该用户类型上联接并以独占方式绑定到用户的分支,然后基于项的所有者绑定到项表。你得到一个结果。
最后,使用类似的join,但仅基于用户的admin类型,并基于项目的admin id加入到项目中。
同样,如果没有您的示例源记录和预期的输出,这是目前我能想到的最好的解释(可能还有其他解释)。因此,对于类似列结果结构中的查询,它们可以通过一个“联合”组合在一起。我添加了一个额外的列来显示数字的来源,分别基于owner/admin的来源。

select
        b.br_name,
        'By Owner' Source,
        count(i.it_id) numOfItems,
        SUM(i.it_amount) sumOfAmount
    FROM
        branch b
            JOIN Users u
                on u.user_type = 'owner'
                AND b.br_id = u.user_branch
                JOIN Item i
                    on u.user_id = i.it_owner
                    AND i.it_activated = 1
    group by
        b.br_name
UNION        
select
        b.br_name,
        'By Admin' Source,
        count(i.it_id) numOfItems,
        SUM(i.it_amount) sumOfAmount
    FROM
        branch b
            JOIN Users u
                on u.user_type = 'admin'
                AND FIND_IN_SET(b.br_id, u.user_branches) > 0
                JOIN Item i
                    on u.user_id = i.it_admin
                    AND i.it_activated = 1
    group by
        b.br_name;

相关问题