前天碰到一业务需求难倒了团队成员 表结构如下 CREATE TABLE [dbo][Product]( [P_ID] [bigint] IDENTITY() NOT NULL [P_Name] [nvarchar]() NULL [CategoryID] [int] NULL [CategoryID] [int] NULL [CategoryID] [int] NULL [P_SingleIntro] [nvarchar]() NULL [LoginID] [nvarchar]() NULL CONSTRAINT [PK_Product] PRIMARY KEY CLUSTERED ( [P_ID] ASC ) 需要随机列出表中每位用户(loginid)的一个产品每次列出时随机值不重复 于是考虑用newid() select max(P_ID)as P_IDloginID from product group by loginid order by NewID() 结果每次取到的P_ID都是相同的!不符合需求 再修改如下 select P_IDLoginIDP_NameP_SingleIntro from product where P_ID in ( select ( select top p_id from product as b where bloginid = cloginid order by newid() ) as p_id from ( select top aloginID from product as a group by aloginid order by NewID() ) as c ) 假定取前个用户 |