表A有
name age
张三 20
李四 25
王五 0
赵六 0
-----
表B有
no age
1 25
2 26
3 27
------
比方我要得到一个结果,搜索表A数据,只要年龄为0的,就在表B中随机读取一个。
假如我这样写,select name, case when age='0' then (select top 1 age from B Order By NewID() else age end) from A 。这样每次的结果王五和赵六的值都一样,我需要的是每次王五和赵六都是独立的从表B读取一个值。
求解?
name age
张三 20
李四 25
王五 0
赵六 0
-----
表B有
no age
1 25
2 26
3 27
------
比方我要得到一个结果,搜索表A数据,只要年龄为0的,就在表B中随机读取一个。
假如我这样写,select name, case when age='0' then (select top 1 age from B Order By NewID() else age end) from A 。这样每次的结果王五和赵六的值都一样,我需要的是每次王五和赵六都是独立的从表B读取一个值。
求解?
解决方案 »
- 怎么配置"联盟数据库服务器"
- 能同时安装MSSQL2000和2005么?
- 为什么SQL存储过程中有临时表时,前台(vb)不能调用最后一个语句的数据集?
- 求一句 含like关键字 简单的SQL语句 (pubs库titles表中, title notes 含 aa字符串的语句 的记录)
- 一个关于空格的非常奇怪的问题
- 请教高手如何用一条SQL实现这种功能
- 一个sql实际问题
- Attemp to initiate a new SQL Server operation with result pending
- 如何打开和建立.qry文件
- ----不同数据库排序规则下的语句编写
- SQL 从两个表格中匹配时间节点的问题!
- SQL 存储过程会的说下哦,简单查询
这句,我试了一下,平均1、2次就会不同,但是b表数据量多的时候,随机性才提现出来。
union all
select name,age from (select tba.name,tbb.age,row=row_number()over(partition by tba.[name] order by checksum(newid())) from tba,tbb where tba.age=0)t where row=1