这个语句是上次在论坛里提问后修改的,得到的结果是: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楼答案加入根据用户的产品名称进行筛选。

解决方案 »

  1.   

    加在 ORDER BY a.SMT_date DESC 前边AND (SMT_yp.SMT_key = 1) 
    AND SMT.SMT_cpname like'%"&饮料&"%'
    ORDER BY a.SMT_date DESC"
      

  2.   


    SELECT 

    FROM 
    SMT_cp a 
    INNER JOIN 
    SMT_yp 
    ON 
    a.SMT_yp_id = SMT_yp.SMT_id 
    WHERE 
    NOT EXISTS (
    SELECT 

    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
      

  3.   

    谢谢TravyLee,上面那个我测试过,不对,我把测试数据给您 
    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   饮料        秋雨
      

  4.   


    --> 测试数据:[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 饮料 小黄
    */
     



      

  5.   

    谢谢,昨天家里有事出去了,我改动了一下,放在数据库里调试,提示:对象名‘t’无效(SELECT a.SMT_id AS cpid, a.SMT_sortid, a.SMT_cpjm, a.SMT_cpjg, a.SMT_cpjgdw, 
          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
      

  6.   

    我有看懂你上面语句意思是先搜索出符合条件产品,然后在从这个符合条件产品中按照规则进行筛选,不过这个语句这样的顺序我复制在数据库里 with t as报错
      

  7.   

    如果你的数据库是sql2000,用临时表试试
    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))
    )
      

  8.   

    报错,go附近有语法错误,如果去了go,则报数据库中已存在名为't'对象
      

  9.   

    数据库是SQL2000,但是报错,go附近有语法错误,如果去了go,则报数据库中已存在名为't'对象能不能直接在上次这个语句里加改进呢?上次这个语句我已经在网页里用了,只是另外一个页面需要加一个搜索框搜索
    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)))