使用Codeigniter和MySQL从同一个表获取对等列值 [英] Get peer column values from same table using Codeigniter and MySQL

查看:100
本文介绍了使用Codeigniter和MySQL从同一个表获取对等列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果父级ID匹配,如何获取对等列值。在下面的例子中,我将subcat_id作为42,并基于cat_id(6)它应该给我41和43。

How can I get peer column values if parent id matches. In the below example, I'll give subcat_id as 42 and based on cat_id(6) it should give me 41 and 43.

cat_id----subcat_id---name
===========================
5---------40----------ABC
6---------41----------DEF
6---------42----------GHI
6---------43----------JKL

感谢。

推荐答案

这是你在看什么。另外,我建议在表中有一个主键。

Is this what you are looking at. Also I would suggest to have a primary key in the table.

select 
t2.cat_id ,
t2.subcat_id,
t2.name
from test t1
join test t2 on t1.cat_id = t2.cat_id
where t1.subcat_id = 42 AND t2.subcat_id <> 42

DEMO

DEMO

同时结帐 http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/

这篇关于使用Codeigniter和MySQL从同一个表获取对等列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆