如何让 mySql 返回 Select JOIN with Count?

发布于 2024-12-19 05:40:25 字数 1065 浏览 0 评论 0原文

我在使用以下存储过程查询时遇到问题。 我有 3 个表:

**table: prop_details**
prop_id | prop_title
     1  | sun
     2  | moon
     3  | star
     4  | mars

**table: prop_account**
prop_id | acnt_id
1       | 1
2       | 1
3       | 1
4       | 1

**table: prop_unit**
unit_id | prop_id
    1   | 1
    2   | 1
    3   | 1
    4   | 2
    5   | 2
    6   | 3
    7   | 3
    8   | 3

我试图在存储过程中收集以下输出:

prop_id     | prop_title    | acnt_id       | unit_count
        1   | sun           | 1             | 3
        2   | moon          | 1             | 2
        3   | star          | 1             | 3
        4   | mars          | 1             | 0

这是我拥有的 SP,但它只返回 1 行:

PROCEDURE `NewProc`(IN in_acntID int)
BEGIN
SELECT
  *, COUNT(unit_id) AS unitCount
FROM
  prop_units pu
RIGHT JOIN
  prop_details pd
ON
  pu.prop_id = pd.prop_id
RIGHT JOIN
  prop_account pa
ON pd.prop_id = pa.prop_id
WHERE
  pa.acnt_id = in_acntID;
END;

我像这样调用 sp:Call selPropertyByAcntID(@cntID) //@行动ID = 1

I am having issues with the following stored procedure query.
I have 3 tables:

**table: prop_details**
prop_id | prop_title
     1  | sun
     2  | moon
     3  | star
     4  | mars

**table: prop_account**
prop_id | acnt_id
1       | 1
2       | 1
3       | 1
4       | 1

**table: prop_unit**
unit_id | prop_id
    1   | 1
    2   | 1
    3   | 1
    4   | 2
    5   | 2
    6   | 3
    7   | 3
    8   | 3

I am trying to gather the following output in the stored procedure:

prop_id     | prop_title    | acnt_id       | unit_count
        1   | sun           | 1             | 3
        2   | moon          | 1             | 2
        3   | star          | 1             | 3
        4   | mars          | 1             | 0

Here is the SP I have, but it is only returning 1 row:

PROCEDURE `NewProc`(IN in_acntID int)
BEGIN
SELECT
  *, COUNT(unit_id) AS unitCount
FROM
  prop_units pu
RIGHT JOIN
  prop_details pd
ON
  pu.prop_id = pd.prop_id
RIGHT JOIN
  prop_account pa
ON pd.prop_id = pa.prop_id
WHERE
  pa.acnt_id = in_acntID;
END;

I am calling the sp like so: Call selPropertyByAcntID(@cntID) //@acntID = 1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凑诗 2024-12-26 05:40:25

您的选择语句应如下所示:

SELECT d.prop_id, d.prop_title, a.acnt_id
   , SUM(CASE
            WHEN u.prop_id IS NOT NULL THEN 1
            ELSE 0
         END) AS UnitCount
FROM #prop_details AS d
INNER JOIN #prop_account AS a ON d.prop_id = a.prop_id
LEFT JOIN #prop_unit AS u ON d.prop_id = u.prop_id
GROUP BY d.prop_id, d.prop_title, a.acnt_id

Your select statement should look like this:

SELECT d.prop_id, d.prop_title, a.acnt_id
   , SUM(CASE
            WHEN u.prop_id IS NOT NULL THEN 1
            ELSE 0
         END) AS UnitCount
FROM #prop_details AS d
INNER JOIN #prop_account AS a ON d.prop_id = a.prop_id
LEFT JOIN #prop_unit AS u ON d.prop_id = u.prop_id
GROUP BY d.prop_id, d.prop_title, a.acnt_id
一腔孤↑勇 2024-12-26 05:40:25

尝试添加 GROUP BY 子句:

PROCEDURE `NewProc`(IN in_acntID int)
BEGIN
SELECT
  *, COUNT(unit_id) AS unitCount
FROM
  prop_units pu
RIGHT JOIN
  prop_details pd
ON
  pu.prop_id = pd.prop_id
RIGHT JOIN
  prop_account pa
ON pd.prop_id = pa.prop_id
WHERE
  pa.acnt_id = in_acntID
GROUP BY pu.prop_id;
END;

Try adding a GROUP BY clause:

PROCEDURE `NewProc`(IN in_acntID int)
BEGIN
SELECT
  *, COUNT(unit_id) AS unitCount
FROM
  prop_units pu
RIGHT JOIN
  prop_details pd
ON
  pu.prop_id = pd.prop_id
RIGHT JOIN
  prop_account pa
ON pd.prop_id = pa.prop_id
WHERE
  pa.acnt_id = in_acntID
GROUP BY pu.prop_id;
END;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文