SQL Server简单操作

https://www.nowcoder.com/practice/4c8b4a10ca5b44189e411107e1d8bec1

一、

1.增

2.删、改

3.查

4.内联

5.左联、右联

 

****cast(@id as varchar)强制转换

……..

5.游标

静态游标static 向上(prior)、向下(next)、第一个(first)、最后一个(last)

静态游标可以使用绝对的定位和对定位

fetch absolute 5 from cursor_userinfo into @userid,@username,@telephone….

 

create创建//alter修改

二、

1.子查询:把一个查询的结果在另一个查询中使用就叫做子查询

select max(hire_date) from employees

select * from employees

where hire_date =

(select max(hire_date) from employees)

2. limit m,n : 表示从第m+1条开始,取n条数据;

limit n : 表示从第0条开始,取n条数据,是limit(0,n)的缩写。

(1)select * from employees order by hire_date desc limit 1

(2)select * from employees order by hire_date desc limit 2,1

(3)select * from employees

where hire_date = select hire_date from employees order by hire_date desc limit 2,1);

3. left join(左连接)返回左表中所有的记录,即使在右表中没有记录

rignt join(右连接)返回右表中所有的记录,即使在左表中没有记录

inner join(内连接)返回两个表中完全匹配的结果集

full join(全连接)只要存在匹配,就返回该行,即是leftjoin和right join

select

    salaries.emp_no,

    salaries.salary,

    salaries.from_date,

    salaries.to_date,

    dept_manager.dept_no

from salaries

join dept_manager on  salaries.emp_no=dept_manager.emp_no

where salaries.to_date='9999-01-01' and dept_manager.to_date='9999-01-01'

4. 注意on与where有什么区别,两个表连接时用on,在使用left  jion时,on和where条件的区别如下:

(1)、on条件是在生成临时表时使用的条件,它不管on中的条件是否为真,都会返回左边表中的记录。

(2)、where条件是在临时表生成好后,再对临时表进行过滤的条件。这时已经没有left  join的含义(必须返回左边表的记录)了,条件不为真的就全部过滤掉

5. distinct用于返回唯一不同的值

select title,count(distinct emp_no) as t from titles

group by title

having t>=2

6. 多次连接嵌套

select e.last_name,e.first_name,d.dept_name from employees as e

left join dept_emp as de on e.emp_no=de.emp_no

left join departments as d on de.dept_no=d.dept_no

 

总结:insert、delete、update、select

      where、 order by、desc、asc、group by having

      inner join、left join、right join、full join

      limit、distinct、max、count、min、sum

cursor、子查询

posted @ 2020-06-07 18:06  吃饭睡觉打痘痘  阅读(96)  评论(0编辑  收藏  举报