sql="select SMT_cp.SMT_id as cpid,SMT_cp.SMT_date,SMT_cp.SMT_cpjg,SMT_cp.SMT_cpname,SMT_cp.SMT_yp_id,SMT_yp.SMT_id,SMT_yp.SMT_coname,SMT_yp.SMT_vip,0 as px from SMT_cp,SMT_yp where SMT_cp.SMT_id in (select max(SMT_cp.SMT_id) from SMT_cp Where 1=1 group by SMT_cp.SMT_yp_id )and SMT_cp.SMT_yp_id=SMT_yp.SMT_id union all select SMT_cp.SMT_id as cpid,SMT_cp.SMT_date,SMT_cp.SMT_cpjg,SMT_cp.SMT_cpname,SMT_cp.SMT_yp_id,SMT_yp.SMT_id,SMT_yp.SMT_coname,SMT_yp.SMT_vip,1 as px from SMT_cp,SMT_yp where SMT_cp.SMT_yp_id=SMT_yp.SMT_id order by px,SMT_yp.paixu,SMT_cp.SMT_date DESC"
需要修改的应该是这个部分:where SMT_cp.SMT_id in (select max(SMT_cp.SMT_id) from SMT_cp Where 1=1 group by SMT_cp.SMT_yp_id ) 请问各位大侠该怎么修改
希望能完整的看一下我的整个问题描述,帮帮忙,能够根据上面表的字段进行修改,万分感谢!
在线等待您的回复!
------解决方案--------------------
SQL code
select * from
(
select [color=#FF0000]top 1[/color] SMT_cp.SMT_id as cpid,SMT_cp.SMT_date,SMT_cp.SMT_cpjg,SMT_cp.SMT_cpname,SMT_cp.SMT_yp_id,SMT_yp.SMT_id,SMT_yp.SMT_coname,SMT_yp.SMT_vip,0 as px from SMT_cp,SMT_yp where SMT_cp.SMT_id in (select max(SMT_cp.SMT_id) from SMT_cp
[color=#FF0000]order by SMT_date desc, SMT_id desc[/color])
union all
....
------解决方案--------------------
SQL code
--查询出每个用户产品发布时间SMT_date最新的一条产品,以SMT_id作为决胜属性
select sc.SMT_id as cpid,sc.SMT_date,sc.SMT_cpjg,sc.SMT_cpname,sc.SMT_yp_id,
sy.SMT_id,sy.SMT_coname,sy.SMT_vip,0 as px from SMT_cp sc,SMT_yp sy
where sy.SMT_id=sc.SMT_yp_id
and sc.SMT_id=(
select top 1 sc1.SMT_id from SMT_cp sc1
where sc1.SMT_id=sc.SMT_id and sc1.SMT_yp_id=sy.SMT_id
order by sc1.SMT_date desc,sc1.SMT_id desc)
union all
/*
查询每个用户发布的所有产品显示,根据用户发布产品的时间、用户等级进行排序
因为在派生表里如果要排序的话,必须要指定top子句,所以top 2147483647可以确保结果集满足排序要求又可以查询出所有结果集
*/
select * from (select top 2147483647 SMT_cp.SMT_id as cpid,SMT_cp.SMT_date,SMT_cp.SMT_cpjg,SMT_cp.SMT_cpname,SMT_cp.SMT_yp_id,
SMT_yp.SMT_id,SMT_yp.SMT_coname,SMT_yp.SMT_vip,1 as px
from SMT_cp,SMT_yp
where SMT_cp.SMT_yp_id=SMT_yp.SMT_id order by SMT_cp.SMT_date desc,SMT_yp.SMT_vip desc)as T
------解决方案--------------------
------解决方案--------------------
SQL code
select
cp.SMT_id as cpid,
cp.SMT_date,
cp.cp.g,
cp.cp.ame,
cp.yp.id,
yp.SMT_id,
yp.SMT_coname,
yp.SMT_vip,
case when ls.SMT_yp_id is not null then 1 else 0 end as px
from SMT_cp cp
join SMT_yp yp on 1=1
and cp.SMT_yp_id=yp.SMT_id
left join (
select SMT_yp_id, max(SMT_id) as last_cp
from SMT_cp
group by SMT_yp_id
) as ls on 1=1
and ls.SMT_yp_id = yp.SMT_id
and last_cp = cp.SMT_id
order by px, cp.SMT_date, yp.SMT_vip