select id,demo,..,..,name from content where id in( select top 10 yid from index_contents where con2 = '设计' and yid in( select yid from index_contents where con2 = '浙江' and yid in( select yid from index_contents where con2 = '公司' )))求优化 条件多了就很慢很慢 尤其是超多5个
子查询的index_contents表结构是这样的 查询的是CON2同时满足条件的相同YID值
yid con2
1 公司
1 设计
2 速度
4 飞机
1 浙江
3 地方
比如con2=浙江 又con2=公司的相同YID
子查询的index_contents表结构是这样的 查询的是CON2同时满足条件的相同YID值
yid con2
1 公司
1 设计
2 速度
4 飞机
1 浙江
3 地方
比如con2=浙江 又con2=公司的相同YID
select yid
from tb
where con2 in ('浙江','公司')
group by yid
--having count(*) >= 2
select id,demo,..,..,name
from content a
inner join index_contents b on a.id=b.yid
where b.con2 in('公司','浙江','设计'))
-- Author :fredrickhu(小F,向高手学习)
-- Date :2011-11-17 09:40:51
-- Version:
-- Microsoft SQL Server 2008 R2 (RTM) - 10.50.1617.0 (Intel X86)
-- Apr 22 2011 11:57:00
-- Copyright (c) Microsoft Corporation
-- Enterprise Evaluation Edition on Windows NT 6.1 <X64> (Build 7600: ) (WOW64)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([yid] int,[con2] varchar(4))
insert [tb]
select 1,'公司' union all
select 1,'设计' union all
select 2,'速度' union all
select 4,'飞机' union all
select 1,'浙江' union all
select 3,'地方'
--------------开始查询--------------------------
select
yid
from
tb t
where
exists(select 1 from tb where yid=t.yid and con2<>t.con2) and con2 in('公司','浙江')
group by
yid ----------------结果----------------------------
/* yid
-----------
1(1 行受影响)
*/
SELECT
a.*
FROM
[content] AS a
INNER JOIN
(SELECT yid FROM index_contents WHERE con2 IN( '浙江', '设计','公司') GROUP BY yid HAVING COUNT(DISTINCT con2)=3)as b ON a.ID=b.yid
create table tb(yid int,con2 varchar(10))
insert into tb
select 1 ,'公司' union all
select 1 ,'设计' union all
select 2 ,'速度' union all
select 4 ,'飞机' union all
select 1 ,'浙江' union all
select 2 ,'浙江' union all
select 3 ,'地方' union all
select 3 ,'公司'
go--将查询条件里的所有作为一个临时表,然后去匹配
;with cte as
(
select '公司' as row
union all
select '浙江'
)select a.yid
from tb a join cte b on a.con2 = b.row
group by a.yid
having count(a.yid) = (select count(*) from cte)drop table tb/**********************yid
-----------
1(1 行受影响)
试了下13亿的数据 直接超时了 不能count 一定要用索引
由于<> 表扫描了 占78%
如何将数据库正文字段中 <p ******> 这样的html标记批量替换为 <p> 去掉其中的样式说明 ***** 内容不是固定格式(如<p class=""> <p id=***> <p class=**> <p id=54>等等)求能批量操作示例的语句。