SQL 连接与计数不起作用
SELECT p.PostPID, p.PostUID, p.PostText, p.PostTime, u.UserUID, u.UserName, u.UserImage, u.UserRep,
(
SELECT COUNT(f.FlagTime)
FROM Flags as f
JOIN Posts as p
ON p.PostPID = f.FlagPID
) as PostFlags
FROM Posts AS p
JOIN Users AS u
ON p.PostUID = u.UserUID
ORDER BY PostTime DESC
LIMIT 0, 30
我有这个查询,我不知道为什么,但这段代码似乎不起作用,它返回查询就像这样:
SELECT p.PostPID, p.PostUID, p.PostText, p.PostTime, u.UserUID, u.UserName, u.UserImage, u.UserRep,
(
SELECT COUNT(f.FlagTime)
FROM Flags as f
) as PostFlags
FROM Posts AS p
JOIN Users AS u
ON p.PostUID = u.UserUID
ORDER BY PostTime DESC
LIMIT 0, 30
我做错了什么?
SELECT p.PostPID, p.PostUID, p.PostText, p.PostTime, u.UserUID, u.UserName, u.UserImage, u.UserRep,
(
SELECT COUNT(f.FlagTime)
FROM Flags as f
JOIN Posts as p
ON p.PostPID = f.FlagPID
) as PostFlags
FROM Posts AS p
JOIN Users AS u
ON p.PostUID = u.UserUID
ORDER BY PostTime DESC
LIMIT 0, 30
I have this query and I don't know why but this piece of code seems not to be working, it returns the query like if it is like this:
SELECT p.PostPID, p.PostUID, p.PostText, p.PostTime, u.UserUID, u.UserName, u.UserImage, u.UserRep,
(
SELECT COUNT(f.FlagTime)
FROM Flags as f
) as PostFlags
FROM Posts AS p
JOIN Users AS u
ON p.PostUID = u.UserUID
ORDER BY PostTime DESC
LIMIT 0, 30
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑:
我认为这样会更好:
Edit:
I think it would be better like this:
如果没有示例数据、示例输出和所需输出的描述,就很难确定您需要什么。
然而,我注意到的是,您的子查询 (PostFlags) 包含表 Posts,主查询也是如此。他们甚至共享相同的别名。
由此我推断您想要获得两件事之一......
所有帖子的所有标志的计数
这将为结果中的每条记录提供相同的值
当前帖子的所有标志的计数
这将为结果中的每条记录提供不同的值
如果您想要版本一,我会更改子查询的别名。不要使用“P”,而使用“Px”或其他东西代替。我不认为这一定会改变任何东西,但是使用与主查询相同的别名可能会让人类读者感到困惑,更不用说 RDBMS 了。
如果您想要版本二,则不需要在子查询中包含联接。相反,您可以执行以下操作...
where 子句现在从主查询引用 Posts 表。这成为一个相关子查询 - 结果根据另一个表中字段的值而变化。
如果这些都不是您所需要的,请举例;
- 示例数据
- 结果示例
- 预期结果
- 描述为什么这些是预期的
Without example data, example output, and a description of the desired out, it's hard to be sure what you need.
What I do notice, however, is that your sub query (PostFlags) contains the table Posts, as does your main query. And they even share the same alias.
From this I'd infer that you want to get one of two things...
The count of all flags for all posts
This would give the same value for every record in your results
The count of all flags for the current post
This would give a different value for each record in your results
If you want version one, I would change the aliases of the sub query. Instead of using "P", use "Px" or something instead. I don't imagine this would necessarily change anything, but using the same alias as the main query could confuse the human reader, never mind the RDBMS.
If you want version two, you don't need to include the join in the sub query. Instead you can do the following...
The where clause is now referencing the Posts table from the main query. This becomes a Correlated-Sub-Query - The result varies dependant upon the value of a field in another table.
If none of these are what you need, please give examples;
- Example data
- Example results
- Expected results
- Description of why these are expected