我有两个表'Account_Position'
和'Account'
与列'Account_id'
相关联。
对于表'Account_id'
中'Account'
的一个条目,我们在'Account_Position'
表中有多个条目。
我想根据'Account'
表中出现的次数显示'Account_Position'
表的所有记录。
我该如何证明。
提前致谢。
答案 0 :(得分:2)
试试这个,
-- if you are using MSSQL, this query won't work because of
-- asterisk in the select clause (you need to specify all the fields)
-- in the group by clause
-- (but it will work on MySQL)
SELECT a.*, COUNT(b.Account_ID) totalOccurence
FROM Account a
LEFT JOIN Account_Position b
ON a.Account_ID = b.Account_ID
GROUP BY a.Account_ID
OR
-- alternatively, you can use this.
SELECT c.*
FROM Account c
(
SELECT a.Account_ID, COUNT(b.Account_ID) totalOccurence
FROM Account a
LEFT JOIN Account_Position b
ON a.Account_ID = b.Account_ID
GROUP BY a.Account_ID
) d ON c.Account_ID = d.Account_ID
答案 1 :(得分:0)
表1(包含更多id的一行)
col - nchar(10) - 检查过
id - int - 选中
表2
id - int - 已检查
col1 - nchar(50) - 检查过
col2 - nchar(50) - 检查过
col3 - nchar(50) - 选中
尝试以下查询
选择
*
这
Table2
inner join(
select
id as id, count(id) as cnt
from
Table1
group by id
)A on
Table2.id = A.id