这边先给你提供一个动态SQL
select
*
from
table
where
1=1 //这一步是针对所有条件均未满足(where后必须跟子句)
if(name != "" and name!=null){ and name=条件一}
if(sex != "" and sex!=null){ and sex=条件二}
。。。。。。
个人解题思路
第一种:
不额外创建表(连接查询)
首先,将所有条件单独查询并将这些语句内连接
例:
select * from tb as t1
join
(select * from tb where name=条件) as t2
on
t1.id=t2.id
join
(select * from tb where age=条件) as t3
on
t1.id=t2.id
(join........ on .......)
where
t1.sex=条件
然后 将上面的查询结果看作一个表t
select t.*,count(t.id) from t
group by t.id // 以id分组
order by count(t.id) asc //以id数量为降序
limit(0,1) //取第一行值
温馨提示:答案为网友推荐,仅供参考