0

2 つのテーブルが'Account_Position'あり'Account'、列にリンクされています'Account_id'

テーブル内の 1 つのエントリに対して'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つのテーブルを考慮する

Table1 (1 つの ID に対して複数の行を含む)
col - nchar(10) - チェック済み
ID - int - チェック済み

Table2
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 に答える