-1

我有一个主表,它的名称是“user”表,它包含用户的所有信息,并且有两个表“business_list”和“classified_list”。

我想显示所有用户信息,包括表格中的业务 总数和 表格business_list中的分类总数classified_list

用户

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 codeigniter 3.0框架,所以很好,如果有人知道这个结果的codeigniter查询?

4

2 回答 2

2

此查询将为您提供所需的结果。它将表连接到user表和表UNION中,并使用条件聚合来汇总与每个用户关联的企业和分类的数量:business_listclassified_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;

或者,您可以对每个表使用 a LEFT JOIN,从每个表中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 演示

于 2018-10-08T11:50:37.247 回答
1

尝试这样的事情。

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
于 2018-10-08T11:18:47.043 回答