mysql join 3 tables and count
Please look at this image
here is 3 tables , and out i want is
uid from table1 industry fr开发者_JAVA技巧om table 3 of same uid count of fid from table 2 of same uid
like in the sample example output will be 2 records
Thanks
I don't see any relation with table 1. Here's an example using an inner join between the two tables and grouping by the uid:
SELECT
t3.uid,
t3.industry,
count(t2.fid)
FROM
table3 t3
INNER JOIN
table2 t2 ON t3.uid = t2.uid
GROUP BY
t3.uid
Try with this:
SELECT table1.uid,table3.industry,COUNT(table2.fid)
FROM table1
INNER JOIN table3 ON table1.uid=table3.uid
INNER JOIN table2 ON table1.uid=table2.uid
GROUP BY table1.uid, table3.industry
Table1 inner join is useless but could be useful if you'll need to retrieve city or mem_no; in this case, remember to add the field also in GROUP BY clause.
精彩评论