Mysql使用同一查询中的行数据来选择另一行? [英] Mysql use row data in same query to select another row?
本文介绍了Mysql使用同一查询中的行数据来选择另一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我以前从未使用过这样的查询,但必须在不使用 2 个查询的情况下才能做到吗?
Ive never had to use a query like this before but there must be away to do it without using 2 queries?
表:forum_categories
Table : forum_categories
--------------------------------------
-- id ---- parent_id ---- name -------
--------------------------------------
1 0 namehere1
2 1 namehere2
3 0 namehere3
4 1 namehere4
5 3 namehere5
我有子猫的 id,我想为子猫选择行,然后使用相同的查询选择父行,其中 parent_id = id 为父行等.
I have the id for a sub cat and i want to select the row for the subcat then with the same query select the the row for the parent where parent_id = id for the parent row etc.
最好的方法是什么?
推荐答案
你想要的东西
select a.id as child_id, a.name as child_name,
b.id as parent_id, a.name as parent_name
from forum_categories a
inner join forum_categories b
on a.parent_id = b.id
要限制为您的已知 ID,请添加
To restrict to your known id, add
where a.id = knownid
这篇关于Mysql使用同一查询中的行数据来选择另一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文