易截截图软件、单文件、免安装、纯绿色、仅160KB

SQL 运行机制问题高手来

请把我的SQL 代码放到查询分析器里。 然后按我说的运行,告诉我 我问的问题,谢谢高手了
if not object_id('ta') is null
  drop table ta
Go
Create table ta([product] nvarchar(2),[spec] nvarchar(2))
Insert ta
select N'p1',N's1' union all
select N'p1',N's2' union all
select N'p1',N's3' union all
select N'p2',N's5' union all
select N'p2',N's4'
Go
select * 
from ta t 
where [spec] = (select top 1 [spec] from ta where [product]=t.[product] order by newid())

问题1 我把 [spec] = 里的等号换成 in 多次按F5运行后 就会出现条数 0到5条之间为什么。我认为 这时候=号应该和in是一个结果,我top1了, in和 newid 有什么冲突吗?
问题2 我把 = 该成 in 后 我把newid() 该成固定列比如spec F5后 条数就一样了。为什么。 
如果回答的清楚,我追加分。 。谢谢
因为spec有重复值。

[spec]的值有重复

spec有重复值

1 2 楼的, 你运行我的数据了吗? spec 是从s1 到s5 你说的重复值我没懂


引用
这个问题印象中好像有讨论过,一时找不到相关帖子了.

基本这是执行计划的差异造成的

select * from ta t where [spec] = (select top 1 [spec] from ta where [product]=t.[product] order by newid())
|--Filter(WHERE:([tempdb].[dbo].[ta].[spe……
UP

= 跟 in 之间是有差别的.

= 运算符 sql会认为子查询中只会返回一笔数据(如果你没top 1,sql便会在子查询中加上以下判断,防止出错)
 |--Assert(WHERE:(C


相关问答:

jsp链接sql2000的疑问?



type Exception report


message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jas ......

Asp+sql server问题 - Web 开发 / ASP

我一个项目,有个插入操作,具体是这样的:
我有进货信息表。在出货时选择相应的进货信息,输入数量,选择部门后,点保存按钮,由于网络延时,点一下没有反映,于是用户就又点一下,导致一次插入了两条记录:
例: ......

求一SQL - MS-SQL Server / 基础类

tab1 字段:billdate,goodsid,incount,inmoney,outcount,outmoney,endprice,endcount,endamt
tab2 字段:goodsid,goodskind(商品类型)
tab3 字段:goodskind(商品类型),kindname
结果:
得到商品类型在一段时间 ......

如何使用MySQL - MS-SQL Server / 基础类

刚刚接触MySQL,不知道该如何去查看数据,插入数据,创建数据库、表,用哪为前辈能指教一二?

MySQL 5.1参考手册

引用
兄弟你会结贴吗?
mysql 参考手册

baidu

MySQL官方文档 http://dev.mysql.com/doc ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号