如何让 mySql 返回 Select JOIN with Count?
我在使用以下存储过程查询时遇到问题。 我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的选择语句应如下所示:
Your select statement should look like this:
尝试添加
GROUP BY
子句:Try adding a
GROUP BY
clause: