就是 user 表中 有六个字段,任意符合其中两项的就提出来,逻辑怎么写
select * from user where XXX=" " and YYY=" "; User.where(:XXX => " ", :YYY => " ")
试试这个
select (IF(condition1, 1, 0)+ IF(condition3, 1, 0) + IF(condition3, 1, 0)) as cnt from users where condition1 or condition2 or condition3 where cnt > 2
fields = %w[a b c d e f]
fields.map { |a| fields.map { |b| a < b ? "(#{a} != 0 AND #{b} != 0)" : nil } }.inject(&:+).compact.join(" OR ")
在 MySQL 中,如果条件成立,则返回 1,不成立则返回 0,所以可以这样做:
select * from users where ( (name like '%王%') + (phone = '15888882222') + (age > 15) + ...) >= 2;