如何使用 2 tables 加入 master table 以在 2 tables 中获取 master table primary id 的计数以及在 codeigniter 中获取 master table 信息

How to join a master table with 2 tables for getting count of master table primary id in 2 tables and with master table information in codeigniter

我有一个大师table它的名字是"user"table它包含了用户的所有信息并且有两个table"business_list"和"classified_list".

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

用户 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

business_list table

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

classified_list table:

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 查询此结果?

尝试这样的事情。

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

这个查询会给你想要的结果。它将 user table 连接到 business_listclassified_list table 的 UNION 并使用条件聚合对企业数量和与每个用户关联的分类:

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;

或者,您可以对每个 table、每个 table 的 DISTINCT 值使用 LEFT JOIN

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 Demo