ROR+MySql查询-将单个值与单个列中以逗号分隔的值进行比较
在Rails中,我尝试对MySql进行一次查询——在从与层次结构相关的数据库中搜索数据时,我必须在表中传递特定的id 表:层次结构ROR+MySql查询-将单个值与单个列中以逗号分隔的值进行比较,mysql,ruby-on-rails-4,Mysql,Ruby On Rails 4,在Rails中,我尝试对MySql进行一次查询——在从与层次结构相关的数据库中搜索数据时,我必须在表中传递特定的id 表:层次结构 id | parent | name 1 | | Electronics 2 | 1 | iPhone 3 | 1 | Moto G ( Android ) 4 | | Clothes 5 | 4 | Kidz Wear 表:评论 id | hierarchy_
id | parent | name
1 | | Electronics
2 | 1 | iPhone
3 | 1 | Moto G ( Android )
4 | | Clothes
5 | 4 | Kidz Wear
表:评论
id | hierarchy_id | value
1 | 1 | Best electronic products values in this store.
2 | 1,2 | iPhone is always best.
3 | 4 | Cotton Clothes - Cool
4 | 1,3 | MotoG with Android M - Paise wasool
5 | 4,5 | New Collection Good One ...
在这里,当我尝试使用层次结构1搜索数据时,它将只显示一条记录。在这里,我没有获取获取剩余记录的方法,因为如果我选择任何父层次结构,那么子数据应该在那里
若我选择了任何子层次结构,那个么系统应该通过转义父级和同级,只返回和该子级相关的值
立即行动:-
$ select * from comments where hierarchy_id = 1;
id | hierarchy_id | value
1 | 1 | Best electronic products values in this store.
层次结构_id=1的预期输出必须为:-
$ select * from comments where **************************
id | hierarchy_id | value
1 | 1 | Best electronic products values in this store.
2 | 1,2 | iPhone is always best.
3 | 1,3 | MotoG with Android M - Paise wasool
预期输出必须为层次结构\u id=3:-
$ select * from comments where **************************
id | hierarchy_id | value
1 | 1,3 | MotoG with Android M - Paise wasool
请提出一些建议 既然在comments表中有hierarchy\u id,为什么不在查询中使用它呢:
SELECT * FROM comments WHERE hierarchy_id=1
很好的捕捉,打字错误的改变。。。我会更新我的问题。