选择 COUNT 与三个表 - mysql

问题描述:

我正在尝试使用三个表和一个查询(使用 WHERE 条件)选择 COUNT.

I am trying to select the COUNT with three tables with one single query (with WHERE conditions).

这是我无法正常工作的代码.

SELECT t1.count(id) AS car_model_count,t2.count(id) AS list_item_count,t3.count(id) 
    FROM `car_model` AS t1
    INNER JOIN `list_item` AS t2
    INNER JOIN `part_item` AS t3
    WHERE t1.user_id=3;

Possible by using Sub-Query OR UNION is possible to get the COUNT代码> 来自多个表.试试这个查询:

Possible by using Sub-Query OR UNION is possible to get the COUNT from multiple table. Try this query :

SELECT 
    (SELECT count(*) FROM `car_model` WHERE user_id=3 ) AS car_model_count,
    (SELECT COUNT(*) FROM `list_item` WHERE user_id=3) AS list_item_count,
    (SELECT count(*) FROM `part_item` WHERE user_id=3) AS part_item_count;