I have a master table its name is "user" table it contain all information of user and there are two tables "business_list" and "classified_list".
I want to show all user information with total number of business from business_list
table and total no of classifieds from classified_list
table
user table
user_id user_name user_email user_phone
-----------------------------------------------------
001 Jose [email protected] 457855654
002 Tom [email protected] 5464644654
003 Nick [email protected] 4545645644
004 Rose [email protected] 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
I want to display information as
User Name User Email User Phone No Of Business No Of Classified
---------------------------------------------------------------------------------
Jose [email protected] 457855654 2 1
Tom [email protected] 5464644654 0 1
Nick [email protected] 4545645644 2 1
Rose [email protected] 554545441 1 2
So what is the mysql join query for getting this result, I am using php codeigniter 3.0 framework so it is nice, if anyone knows codeigniter query for this result?
This query will give you the results you want. It joins the
user
table to aUNION
of thebusiness_list
andclassified_list
tables and uses conditional aggregation to sum the number of businesses and classifieds associated with each user:Alternatively, you can use a
LEFT JOIN
to each table,COUNT
ingDISTINCT
values from each table:The output of both queries is the same:
SQLFiddle Demo