0

我有两个表'Account_Position''Account'与一个列链接'Account_id'

对于表中的一个条目,我们'Account_id'在表'Account'中有多个条目'Account_Position'

我想根据'Account'表格中出现的次数来显示表格的 所有记录'Account_Position'

我怎么能证明这一点。

提前致谢。

4

2 回答 2

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

或者

-- 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
于 2012-08-24T07:19:32.553 回答
0

考虑2张桌子

表 1 (包含多个 id 的一行)
col - nchar(10) - Checked
id - int - Checked

表 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
于 2012-08-24T11:28:08.227 回答