关于联合查询union all 问题

来源:百度知道 编辑:UC知道 时间:2024/06/20 18:44:35
这样一个查询,用一条SQL语句解决不了:
排序优先级别:关键字匹配>会员级别>产品发布时间
一.关键字匹配的优先级别:
1.产品名称包含关键字and标签包含关键字
2.产品名称包含关键字
3.标签包含关键字
二.会员优先级别:
1.钻石会员
2.黄金会员
3.免费会员
三.按产品发布时间降序排。

哪位大侠给我写的思路?
这个SQL语句是对多表进行查询的,数据量也不小,我本来是写啦3个SQL来实现的,但是效率太低啦。

数据量不大的话逐步整理吧
1 增加排序字段 ord number 。
初始化成0 update xxx set ord=0;
产品名称包含关键字 update xxx set ord=ord+1000 where 产品名称like '%关键字%';
标签包含关键字 update xxx set ord=ord+100 where 标签like '%关键字%';
会员(钻/黄/免对应1/2/3): update xxx set ord=ord+(10-会员优先级别);
select * from xxx order by ord desc,产品发布时间 desc

select * from table where 产品名称包含关键字and标签包含关键字 and 会员 = 钻石会员 order by 产品发布时间desc
union all
select * from table where 产品名称包含关键字and标签包含关键字 and 会员 = 黄金会员 order by 产品发布时间desc
union all
select * from table where 产品名称包含关键字and标签包含关键字 and 会员 = 免费会员 order by 产品发布时间desc
union all
select * from table where 产品名称包含关键字and标签不包含关键字 and 会员 = 钻石会员 order by 产品发布时间desc
union all
select * from table where 产品名称包含关键字and标签不包含关键字 and 会员 = 黄金会员 order by 产品发布时间desc
union all
select * from table where 产品名称包含关键字and标签不包含关键字 and 会员 = 免费会员 order by 产品发布时间desc
union all
select * from table where 产品名称不包含关键字and标签包含关键字