当前位置:Gxlcms > 数据库问题 > 常用SQL语句的整理--SQL server 2008(查询三--子查询)和guid

常用SQL语句的整理--SQL server 2008(查询三--子查询)和guid

时间:2021-07-01 10:21:17 帮助过:3人阅读

ROW_NUMBER() over(order by id)as num,*from gb_data where num>5and num<10--这行代码是错误的,因为系统识别不出来num是多少,为什么呢?

--是因为执行顺序的问题,sql语句是先执行from,然后执行where,最后执行select

--正确的写法如下

  1. <span style="color: #0000ff;">select</span> *<span style="color: #0000ff;">from</span> (<span style="color: #0000ff;">select</span> ROW_NUMBER() over(order by id)<span style="color: #0000ff;">as</span> num,*<span style="color: #0000ff;">from</span> gb_data) <span style="color: #0000ff;">as</span> t <span style="color: #0000ff;">where</span> t.num>5and t.num<<span style="color: #800080;">10</span>

--子查询(以查询的表作为条件,在基础上进行第二次的查询)查询所有书出版的年份入会的读者信息

  1. <span style="color: #0000ff;">select</span>*<span style="color: #0000ff;">from</span> Reader <span style="color: #0000ff;">where</span> FYearOfJoin <span style="color: #0000ff;">in</span>(<span style="color: #0000ff;">select</span> FyearPublished <span style="color: #0000ff;">from</span> Book)

GUID:Server中生成GUID的函数是newid()

  1. insert into t_table (id) values (newid())

 

常用SQL语句的整理--SQL server 2008(查询三--子查询)和guid

标签:

人气教程排行