选择 COUNT 与三个表 - mysql [英] select COUNT with three table - mysql

查看:65
本文介绍了选择 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;

这篇关于选择 COUNT 与三个表 - mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆