急求一条 SQL 语句的写法。

sunon 2004-07-13 11:28:01
我有一个产品表:mi_id 表示注册用户的ID。我要列出所有用户的产品,但每个用户最多只能显示 5 个产品。

请问这条SQL如何写?不能用子查询,因为我的数据库不支持。
...全文
685 29 打赏 收藏 转发到动态 举报
写回复
用AI写文章
29 条回复
切换为时间正序
请发表友善的回复…
发表回复
bflovesnow 2004-07-20
  • 打赏
  • 举报
回复
create temporary table tb(a,b) select a,b from tb2
bflovesnow 2004-07-20
  • 打赏
  • 举报
回复
MySQL 用

create template table tb(a,b) select a,b from tb2

的方式实现

select ... into

的功能
sunon 2004-07-20
  • 打赏
  • 举报
回复
我就是用 create ... select ...的,就是UPDATE 这里不行。好像不能在同一条SQL中用"变量",救命呀!!!!!!!!!!
Jeewins 2004-07-19
  • 打赏
  • 举报
回复
我也见到了这种情况


确实有时候一个临时表能解决很重要的问题
Clove 2004-07-19
  • 打赏
  • 举报
回复
高手啊
tmxkdldw 2004-07-17
  • 打赏
  • 举报
回复
up有分
dededidedu 2004-07-14
  • 打赏
  • 举报
回复
我碰到过类似的问题,因为不支持子查询所以我一般是先取出一个结果集,再在程序里循环取出结果,直接用sql语句在mysql里好像还真没辙。
zonelive 2004-07-14
  • 打赏
  • 举报
回复
不支持子查询,那只能用临时表,说白了,就是用临时表代替了子查询,如果说还不支持临时表的话,那就用一个实表好了,每次在完成后trucate表就行了
waiting520 2004-07-14
  • 打赏
  • 举报
回复
临时表 很有用的 一般很多牵扯到业务问题 都会用到临时表
sunon 2004-07-13
  • 打赏
  • 举报
回复
我用的是MySql 4.0.20 好像没有 SELECT .... INTO table .....
whisht 2004-07-13
  • 打赏
  • 举报
回复
up有分
zjcxc 元老 2004-07-13
  • 打赏
  • 举报
回复
set nocount on --SQL Server 的话,放在一条语句中加上此句
select mid=0,* into #t from 产品表 order by mt_id
declare @i int,@mt_id int
update #t set @i=case @mt_id when mt_id then @i+1 else 1 end
,mid=@i,@mt_id=mt_id
select *
from 用户表 a join #t b on a.mt_id=b.mt_id
where a.usertype=1 or (a.usertype=0 and b.mid<=5)
drop table #t
zjcxc 元老 2004-07-13
  • 打赏
  • 举报
回复
SQL Server可以,其他数据库就不清楚了.
sunon 2004-07-13
  • 打赏
  • 举报
回复
谢谢!以上语句能放在一条SQL中执行吗? 无法用存储过程。
phoenixsharp 2004-07-13
  • 打赏
  • 举报
回复
up.
zjcxc 元老 2004-07-13
  • 打赏
  • 举报
回复
--用临时表就可以解决:

select mid=0,* into #t from 产品表 order by mt_id
declare @i int,@mt_id int
update #t set @i=case @mt_id when mt_id then @i+1 else 1 end
,mid=@i,@mt_id=mt_id
select *
from 用户表 a join #t b on a.mt_id=b.mt_id
where a.usertype=1 or (a.usertype=0 and b.mid<=5)
sunon 2004-07-13
  • 打赏
  • 举报
回复
实际问题是这样的:

用户表: mt_id, username, password, sex, ...., usertype
产品表: id, mt_id, title, ...., postdate

用户表中:usertype 为 0 表示免费用户,为 1 表示收费用户.

免费用户只能显示 5 条产品,收费用户可以显示所有产品。
aoenzh 2004-07-13
  • 打赏
  • 举报
回复
select top 5 产品 group by mi_id,产品
大家见笑了,我胡乱写的
sunon 2004-07-13
  • 打赏
  • 举报
回复
表结构:id, mt_id, title, keyword, details

id:自动编号的
mt_id: 注册用户的Id
title, keyword, details 是字串。

临时表可以用。
zjcxc 元老 2004-07-13
  • 打赏
  • 举报
回复
可以用那些查询,临时表可以用吗?
加载更多回复(9)

34,594

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧