用三个表选择COUNT - mysql

时间:2016-04-08 11:32:05

标签: mysql

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

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

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;

1 个答案:

答案 0 :(得分:1)

使用Sub-Query OR UNION可以从多个表中获取COUNT。 试试这个问题:

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;