比较多个列值 [英] Compare multiple column values

查看:52
本文介绍了比较多个列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我有包含以下数据的表格

Ok, so i have table with following data

product_id      filter_id     
51                 1
52                 2
51                 2
52                 1
52                 4

表格说明

group_id      filter_id     filter_name      group_name
1                 1             white          COlour   
1                 2             black           COlour   
1                 3             grey            COlour   
2                 4             formal          Style  
2                 5             casual         Style   

如果用户给定了一组过滤器 id,我想过滤产品,例如通过将 filter_id 分组考虑到他们的 group_id 例如. select * from filter where filter_id in (1,2) AND filter_id in (4,5) 当我使用这个查询时,我得到空输出

If given set of filter id by user, i want to filter products like by grouping filter_id considering their group_id for ex. select * from filter where filter_id in (1,2) AND filter_id in (4,5) when i use this query i get empty output

为什么我在这里两次使用 IN 是因为 1,2 属于一个 filter_group 而 4,5 属于不同的 filter_group,可能会在表中添加组和过滤器,因此我将根据用户选择使用 php 生成动态查询.

why i used IN twice here is because 1,2 belong to one filter_group and 4,5 belongs to different filter_group, groups and filters may be added in the table so i will be generating dynamic query using php depending on user selection.

谁能帮我找到这个问题的解决方案,在此先感谢您

Can anyone help me out in finding a solution for this problem, Thank you in advance

推荐答案

这个查询对我有用:

select
..
......

where filter.product_id in
  (select a.product_id from filter a where a.filter_id in (1,2))
AND filter.product_id in 
  (select a.product_id from filter a where a.filter_id in(4))

这篇关于比较多个列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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