这个语句是上次在论坛里提问后修改的,得到的结果是:1、从表中每个对应用户都只提取出一条产品显示,优先提取用户最新发布或更新的那条产品,如果用户发布产品时间都是相同的,那么在这样情况下就提取它ID最大产品sql="SELECT * FROM SMT_cp a INNER JOIN SMT_yp ON a.SMT_yp_id = SMT_yp.SMT_id WHERE (NOT EXISTS (SELECT 1 FROM SMT_cp AS b WHERE a.SMT_yp_id = b.SMT_yp_id AND (a.SMT_date < b.SMT_date OR (a.SMT_date = b.SMT_date AND a.SMT_id < b.SMT_id)))) AND(SMT_yp.SMT_key = 1) ORDER BY a.SMT_date DESC"
现在想要加上一个搜素条件(SMT.SMT_cpname like'%"&饮料&"%') ,不知道该加在什么地方才是正确的。
这是上次的帖子:http://topic.csdn.net/u/20120702/16/229204a2-d307-4e31-8dbf-f488f50277a6.html?seed=1212121675&r=79008881#r_79008881 #28楼 TravyLee大哥的回答是正确的,现在就是想根据#28楼答案加入根据用户的产品名称进行筛选。
现在想要加上一个搜素条件(SMT.SMT_cpname like'%"&饮料&"%') ,不知道该加在什么地方才是正确的。
这是上次的帖子:http://topic.csdn.net/u/20120702/16/229204a2-d307-4e31-8dbf-f488f50277a6.html?seed=1212121675&r=79008881#r_79008881 #28楼 TravyLee大哥的回答是正确的,现在就是想根据#28楼答案加入根据用户的产品名称进行筛选。
解决方案 »
- 有没有简单的sql语句查差别最小的记录
- sql 更新一个表中的N条记录,让这N条记录的同一字段从一个值(比如600)开始自增1,十分急
- 求SQL语句实现
- 请教一个简单的脚本,,有关快速找出中断序号的问题
- 为何在存储过程中的这句话不能用?select * from @DatabaseName..syscolumns
- log explorer提示end date after end of log,什么意思
- 这个查询怎么实现,在线等待!!!!!!!!!!!!!!!!!!!!!!
- SQL里面能存储最大容量的是什么类型?有没有比TEXT大的?
- 不知道这是不是交叉表,在线等待,分不够+,只要解决问题就行
- 登录SQL*Plus的问题。。。
- 若干条记录,有部分字段内容一样,想要把相同字段合并成视图中的一条记录,保留不同字段内容。
- 有没有这样的一种解决方案,拜大婶
AND SMT.SMT_cpname like'%"&饮料&"%'
ORDER BY a.SMT_date DESC"
SELECT
*
FROM
SMT_cp a
INNER JOIN
SMT_yp
ON
a.SMT_yp_id = SMT_yp.SMT_id
WHERE
NOT EXISTS (
SELECT
1
FROM
SMT_cp AS b
WHERE
a.SMT_yp_id = b.SMT_yp_id
AND (a.SMT_date < b.SMT_date
OR (a.SMT_date = b.SMT_date AND a.SMT_id < b.SMT_id))))
AND(SMT_yp.SMT_key = 1
AND SMT.SMT_cpname like'%"&饮料&"%'--家在这试试,不对的话你还是把测试数据给出来,再给出结果
ORDER BY
a.SMT_date DESC
SMT_cp表(产品表)产品编号 对应用户ID 发布时间 产品名称
SMT_id SMT_yp_id SMT_data SMT_cpname
1 9 2012-4-12 14:33:36 饮料
2 11 2012-4-18 19:23:36 食品
3 9 2012-4-12 14:33:36 饮料
4 9 2012-4-12 14:33:36 香水
5 12 2012-5-19 19:23:36 饮料
6 11 2012-3-17 19:23:36 饮料
7 12 2012-5-19 19:23:36 饮料
8 8 2012-5-20 19:23:36 香油
9 8 2012-5-17 19:23:36 饮料
10 8 2012-5-18 19:23:36 饮料
11 8 2012-5-16 19:23:36 饮料
12 10 2012-5-19 19:23:36 口服液
13 10 2012-5-19 19:23:36 豆腐
14 13 2012-5-19 19:23:36 保健品
15 13 2012-6-19 16:23:41 饮料
16 14 2012-6-19 16:23:41 饮料
SMT_yp表(用户表)
用户编号 用户名称
SMT_id SMT_coname
8 王明
9 陈丽
10 海风
11 秋雨
12 夏天
13 毛毛
14 小黄这是两个表联在一起查询的,我现在把大概的数据弄了一下,想要得到的查询结果:
产品编号 对应用户ID 发布时间 产品名称 用户名称
16 14 2012-6-19 16:23:41 饮料 小黄
15 13 2012-6-19 16:23:41 饮料 毛毛
7 12 2012-5-19 19:23:36 饮料 夏天
10 8 2012-5-18 19:23:36 饮料 海风
3 9 2012-4-12 14:33:36 饮料 陈丽
6 11 2012-3-17 19:23:36 饮料 秋雨
--> 测试数据:[SMT_cp]
if object_id('[SMT_cp]') is not null
drop table [SMT_cp]
create table [SMT_cp](
[SMT_id] int,
[SMT_yp_id] int,
[SMT_data] datetime,
[SMT_cpname] varchar(6)
)
go
insert [SMT_cp]
select 1,9,'2012-4-12 14:33:36','饮料' union all
select 2,11,'2012-4-18 19:23:36','食品' union all
select 3,9,'2012-4-12 14:33:36','饮料' union all
select 4,9,'2012-4-12 14:33:36','香水' union all
select 5,12,'2012-5-19 19:23:36','饮料' union all
select 6,11,'2012-3-17 19:23:36','饮料' union all
select 7,12,'2012-5-19 19:23:36','饮料' union all
select 8,8,'2012-5-20 19:23:36','香油' union all
select 9,8,'2012-5-17 19:23:36','饮料' union all
select 10,8,'2012-5-18 19:23:36','饮料' union all
select 11,8,'2012-5-16 19:23:36','饮料' union all
select 12,10,'2012-5-19 19:23:36','口服液' union all
select 13,10,'2012-5-19 19:23:36','豆腐' union all
select 14,13,'2012-5-19 19:23:36','保健品' union all
select 15,13,'2012-6-19 16:23:41','饮料' union all
select 16,14,'2012-6-19 16:23:41','饮料'
go
--> 测试数据:[SMT_yp]
if object_id('[SMT_yp]') is not null
drop table [SMT_yp]
create table [SMT_yp](
[SMT_id] int,
[SMT_coname] varchar(4)
)
go
insert [SMT_yp]
select 8,'王明' union all
select 9,'陈丽' union all
select 10,'海风' union all
select 11,'秋雨' union all
select 12,'夏天' union all
select 13,'毛毛' union all
select 14,'小黄'
go;with t
as(
select
a.*,
b.SMT_coname
from
[SMT_cp] a
inner join
[SMT_yp] b
on
a.SMT_yp_id=b.SMT_id
where
charindex('饮料',a.SMT_cpname)>0
)
select
*
from
t a
where
not exists(
select
1
from
t b
where
a.SMT_yp_id=b.SMT_yp_id
and (
(a.SMT_data<b.SMT_data)
or (a.SMT_data=b.SMT_data and a.SMT_id<b.SMT_id)
)
)
/*
SMT_id SMT_yp_id SMT_data SMT_cpname SMT_coname
------------------------------------------------------------------
10 8 2012-05-18 19:23:36.000 饮料 王明
3 9 2012-04-12 14:33:36.000 饮料 陈丽
6 11 2012-03-17 19:23:36.000 饮料 秋雨
7 12 2012-05-19 19:23:36.000 饮料 夏天
15 13 2012-06-19 16:23:41.000 饮料 毛毛
16 14 2012-06-19 16:23:41.000 饮料 小黄
*/
a.SMT_cpname, b.SMT_coname, a.SMT_cpjl, a.SMT_yp_id, a.SMT_pic, b.paixu,
b.SMT_id, b.SMT_user, b.SMT_qyqq, b.SMT_addtwo_id, b.SMT_co, b.SMT_vip,
a.SMT_date
FROM SMT_cp a INNER JOIN
SMT_yp b ON a.SMT_yp_id = b.SMT_id
WHERE charindex('消毒液', a.SMT_cpname) > 0 AND (b.SMT_vip <= 5) AND
(b.SMT_key = 1))
SELECT *
FROM t a
WHERE NOT EXISTS
(SELECT 1
FROM t b
WHERE a.SMT_yp_id = b.SMT_yp_id AND ((a.SMT_data < b.SMT_data) OR
(a.SMT_data = b.SMT_data AND a.SMT_id < b.SMT_id)))
ORDER BY SMT_date DESC
select a.*,b.SMT_coname
into #t
from [SMT_cp] a
inner join [SMT_yp] b
on a.SMT_yp_id=b.SMT_id
where charindex('饮料',a.SMT_cpname)>0
goselect *
from #t a
where not exists(
select 1 from #t b
where a.SMT_yp_id=b.SMT_yp_id
and ((a.SMT_data<b.SMT_data) or (a.SMT_data=b.SMT_data and a.SMT_id<b.SMT_id))
)
select * from test as a
where not exists(select 1 from test as b
where a.dyid=b.dyid and (a.SMT_data<b.SMT_data
or (a.SMT_data=b.SMT_data and a.SMT_id<b.SMT_id)))