1

次のようなクエリがあります..

SELECT id_b FROM id_table
WHERE id_a = ?
AND (
       SELECT COUNT(*) AS rowCount FROM other_table
       WHERE id = id_b
     )  > 0;

値rowCountを取得するにはどうすればよいですか?

編集: これは MYSQL DB です

4

2 に答える 2

1
Select A..., Z.RowCount
From id_table As A
    Cross Join  (
                Select Count(*) As RowCount
                From other_table
                Where id_b = ?
                ) As Z
Where A.id_a = ?
    And Z.RowCount > 0

編集

あなたの編集を考えると、あなたは次のようなことを達成しようとしていると思います:

Select A..., Z.RowCount
From id_table As A
    Join    (
            Select id_b, Count(*) As RowCount
            From other_table
            Group By id_b
            ) As Z
        On Z.id_b = A.id_b
Where A.id_a = ?
    And Z.Cnt > 0
于 2012-06-14T04:58:57.257 に答える
0

あなたは試すことができます:

SELECT (
       SELECT COUNT(*) AS rowCount FROM other_table
       WHERE id_b = ?
     ) AS rowCount, id_b FROM id_table
WHERE id_a = ?
AND (
       SELECT COUNT(*) AS rowCount FROM other_table
       WHERE id_b = ?
     )  > 0;

それはSQL Serverで機能します

于 2012-06-14T04:43:03.513 に答える