本文介绍了在sql查询中添加第三个查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下查询从两个表中获取记录。现在我想以这样的方式编辑我的查询,它也从第三个表中获取记录。我的第三个查询将类似于table3中的select count(*),其中user_id = user_id。



我不擅长使用连接。这就是我问这个问题的原因。

i am using the below query to fetch record from two tables. Now i want to edit my query in such a way that it fetch record also from third table. my third query will be like select count(*) from table3 where user_id=user_id.

I am not good in using joins. That's why i am asking this question.

SELECT table1.displayname,table2.title
FROM table1
INNER JOIN table2 ON talbe2.user_id = table1.userid



任何帮助都将不胜感激。


Any help would be appreciated.

推荐答案

SELECT table1.displayname,table2.title ,count(table3.user_id)
FROM table1
INNER JOIN table2 ON table2.user_id = table1.userid inner join table2 on table3.user_id = table1.userid group by table1.displayname,table2.title





使用聚合函数时需要分组依据



you need Group by clause when you use aggregate function


SELECT
    T1.displayname
    ,T2.title
    ,COUNT(T3.user_id) AS No_of_records_in_Table3
FROM Table1 T1
INNER JOIN Table2 T2
    ON T2.user_id = T1.userid
INNER JOIN Table3 T3
    ON T3.user_id = T1.userid

GROUP BY T1.displayname, T2.title



您还应该阅读。

SELECT table1.displayname,table2.title, (SELECT COUNT(*) FROM Table3 WHERE table1.userid = Table3.userid) AS CountOfSomething
FROM table1
INNER JOIN table2 ON talbe2.user_id = table1.userid





不要害怕加入!请参阅此文章: []


这篇关于在sql查询中添加第三个查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-30 02:27
查看更多