如何在 MySQL 的子查询中指定父查询字段?
如何在 MySQL 的子查询中指定父查询字段?
例如:
我用 PHP 编写了一个基本的公告板类型程序。
在数据库中,每个帖子包含:id(PK)和parent_id(父帖子的id)。如果帖子本身是父帖子,则其parent_id 设置为 0。
我正在尝试编写一个 mySQL 查询,该查询将找到每个父帖子以及父帖子所拥有的子帖子数量。
$query = "SELECT id, (
SELECT COUNT(1)
FROM post_table
WHERE parent_id = id
) as num_children
FROM post_table
WHERE parent_id = 0";
棘手的部分是第一个 id 不知道它应该引用子查询之外的第二个 id。我知道我可以执行 SELECT id AS id_tmp ,然后在子查询中引用它,但是如果我还想返回 id 并保留“id”作为列名,那么我必须执行一个返回的查询我有两列具有相同的数据(这对我来说似乎很混乱)
$query = "SELECT id, id AS id_tmp,
(SELECT COUNT(1)
FROM post_table
WHERE parent_id = id_tmp) as num_children
FROM post_table
WHERE parent_id = 0";
这种混乱的方式工作得很好,但我觉得有机会在这里学习一些东西,所以我想我应该发布这个问题。
How do I specify the parent query field from within a subquery in MySQL?
For Example:
I have written a basic Bulletin Board type program in PHP.
In the database each post contains: id(PK) and parent_id(the id of the parent post). If the post is itself a parent, then its parent_id is set to 0.
I am trying to write a mySQL query that will find every parent post and the number of children that the parent has.
$query = "SELECT id, (
SELECT COUNT(1)
FROM post_table
WHERE parent_id = id
) as num_children
FROM post_table
WHERE parent_id = 0";
The tricky part is that the first id doesn't know that it should be referring to the second id that is outside of the subquery. I know that I can do SELECT id AS id_tmp and then refer to it inside the subquery, but then if I want to also return the id and keep "id" as the column name, then I'd have to do a query that returns me 2 columns with the same data (which seems messy to me)
$query = "SELECT id, id AS id_tmp,
(SELECT COUNT(1)
FROM post_table
WHERE parent_id = id_tmp) as num_children
FROM post_table
WHERE parent_id = 0";
The messy way works fine, but I feel an opportunity to learn something here so I thought I'd post the question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
怎么样:
或者如果你在 p1.id 上添加一个别名,你可能会说:
How about:
or if you put an alias on the p1.id, you might say:
您可以尝试类似的操作
要回到您的示例,请在子选择中使用主表的别名
You could try something like this
To get back to your example, use the alias of the main table in the sub select
为表指定唯一的名称:
Give the tables unique names:
以下语法适用于 Oracle。你能测试一下在 MYSQL 中是否也同样有效吗?
在 Oracle 中称为标量子查询。
如果您两次使用同一个表,您只需要为两个表添加不同的别名即可区分它们。
The following syntax works in Oracle. Can you test if the same works in MYSQL too?
It is called scalar subquery in Oracle.
You would just need to alias the two tables differently to distinguish between them if you are using the same table twice.
谢谢唐。我有一个如下所示的嵌套查询,其中的
WHERE
子句无法确定别名v1
。这是不起作用的代码:因此,我只是在
JOIN
中再次添加了别名v1
。这使它发挥作用。希望这对某人有帮助。
Thanks Don. I had a nested query as shown below and a
WHERE
clause in it wasn't able to determine aliasv1
. Here is the code which isn't working:So, I just added the alias
v1
again inside theJOIN
. Which made it work.Hope this will be helpful for someone.
MySQL 8 中子查询中的父查询字段。
我使用嵌套查询根据 tblgamescores 中的用户名选择游戏分数。
Parent query field within a subquery in MySQL 8.
I'm selecing games scores on the basis of username from tblgamescores using nested query.