1

在购买表中。当客户购买同一本书时,将 1 行添加到表中。所以当在购买页面看到购买清单时,它是这样的:

buy1 price1 downloadlink1
buy1 price1 downloadlink1
buy1 price1 downloadlink1
buy2 price2 downloadlink2
buy2 price2 downloadlink2
buy2 price2 downloadlink2

但我想在同一行价格和下载链接删除,只有第一行显示这个值是这样的:

buy1 price1 downloadlink1
buy1 
buy1 
buy2 price2 downloadlink2
buy2 
buy2 

我怎样才能使用sql查询???

4

3 回答 3

1

好吧,如果它是 MS SQL 服务器,我可以建议使用row_number over() 但是在你的情况下,我可以建议试试这个(我想你的表中有一些 id)

select
    A.sub,
    case when A.frst = 1 then A.user else null end as user,
    case when A.frst = 1 then A.versionid else null end as versionid,
    case when A.frst = 1 then A.hard else null end as hard,
    case when A.frst = 1 then A.active else null end as active,
    case when A.frst = 1 then A.res else null end as res
from
(
    select
        *,
        case
            when exists
            (
                select * 
                from buy as tt
                where
                    tt.sub = t.sub and tt.res = t.res and
                    tt.user = t.user and tt.versionid = t.versionid and
                    tt.hard = t.hard and tt.active = t.active and
                    tt.id < t.id
            ) then 0
            else 1
        end as frst
    from buy as t
) as A
order by A.sub, A.frst desc

SQL 小提琴

于 2012-11-28T08:33:44.990 回答
0

distinct在执行下载链接时在 sql 查询中使用

于 2012-11-28T08:23:59.107 回答
0

如果该用户未将特定项目添加到表中,则将行添加到表中

如果该项目已经存在(使用项目 ID 和用户 ID 检查)更新表

于 2012-11-28T08:24:32.553 回答