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 数据库

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 回答