如何将一个主表与两个表连接起来,以获取两个表中主表主id的计数,以及codeigniter中的主表信息

rm5edbpk  于 2021-06-18  发布在  Mysql
关注(0)|答案(2)|浏览(441)

我有一个主表,它的名称是“用户”表,它包含用户的所有信息,有两个表“业务列表”和“分类列表”。
我想显示所有的用户信息与业务总数从 business_list 表和来自 classified_list table
用户表

user_id    user_name     user_email         user_phone
-----------------------------------------------------     
001         Jose         jose@yahoo.in      457855654
002         Tom          tom@yahoo.in       5464644654
003         Nick         nick@yahoo.in      4545645644
004         Rose         rose@yahoo.in      554545441

业务列表

bid        user_id      business_name
-----------------------------------------------
001        001          Construction business
002        003          Event business
003        001          Crane business
004        003          Furtinure business
005        004          Realestate business

分类列表:

cid      user_id       classified_name
-------------------------------------------    
001      001           Roller classified
002      004           Home classified
003      003           Chair classified
004      004           Office Classified
005      002           Light decoration classified

我想把信息显示为

User Name   User Email         User Phone     No Of Business     No Of Classified
---------------------------------------------------------------------------------
Jose        jose@yahoo.in      457855654           2                   1
Tom         tom@yahoo.in       5464644654          0                   1
Nick        nick@yahoo.in      4545645644          2                   1
Rose        rose@yahoo.in      554545441           1                   2

那么得到这个结果的mysql连接查询是什么呢?我使用的是php codeigniter3.0框架,所以很好,如果有人知道codeigniter查询这个结果的话?

rsl1atfo

rsl1atfo1#

试试这样的。

select u.user_name, u.user_email, u.user_phone, count(distinct b.bid),
       count(distinct c.cid) from user_table u 
   left outer join business_list b 
       on  u.user_id=b.user_id 
   left outer join classified_list c 
       on c.user_id=u.user_id 
group by 
    u.user_name,u.user_email,u.user_phone
ymzxtsji

ymzxtsji2#

此查询将提供所需的结果。它加入了 user 表到a UNIONbusiness_list 以及 classified_list 表,并使用条件聚合对与每个用户关联的业务和分类的数量求和:

SELECT u.user_name AS `User Name`,
       u.user_email AS `User Email`,
       u.user_phone AS `User Phone`,
       SUM(CASE WHEN bc.type = 'business' THEN 1 ELSE 0 END) AS `No Of Business`,
       SUM(CASE WHEN bc.type = 'classified' THEN 1 ELSE 0 END) AS `No Of Classified`
FROM user u
JOIN (SELECT 'business' AS type, user_id 
      FROM business_list
      UNION ALL
      SELECT 'classified' AS type, user_id
      FROM classified_list) bc
ON bc.user_id = u.user_id
GROUP BY u.user_id;

或者,您可以使用 LEFT JOIN 每一张table, COUNT 惯性导航与制导 DISTINCT 每个表中的值:

SELECT u.user_name AS `User Name`,
       u.user_email AS `User Email`,
       u.user_phone AS `User Phone`,
       COUNT(DISTINCT b.bid) AS `No Of Business`,
       COUNT(DISTINCT c.cid) AS `No Of Classified`
FROM user u
LEFT JOIN business_list b ON b.user_id = u.user_id
LEFT JOIN classified_list c ON c.user_id = u.user_id
GROUP BY u.user_id

两个查询的输出相同:

User Name   User Email      User Phone  No Of Business  No Of Classified
Jose        jose@yahoo.in   457855654   2               1
Tom         tom@yahoo.in    5464644654  0               1
Nick        nick@yahoo.in   4545645644  2               1
Rose        rose@yahoo.in   554545441   1               2

sqlfiddle演示

相关问题