sql 备忘
**************************************
**** sql 查询备忘 ****
**************************************
一:外连接:
1、左外连接(把join左边表里的所有数据都查出来。然后把join 右边表中的符合条件的数据加在左边表的后面。。。。)
SELECT * from t_empl_info as a LEFT OUTER JOIN t_dept as b
ON a.dept_no=b.dept_id
--通常情况下是:左边表是多方表。右边表是一方表
--三张表的左外连接
SELECT *
from A left join B
on A.a=B.a
left join C on B.b = C.b;
相关文档:
--1. 创建表,添加测试数据
CREATE TABLE tb(id int, [value] varchar(10))
INSERT tb SELECT 1, 'aa'
UNION ALL SELECT 1, 'bb'
UNION ALL SELECT 2, 'aaa'
UNION ALL SELECT 2, 'bbb'
UNION ALL SELECT 2, 'ccc'
--SELECT * from tb
/**//*
id value
----------- ----------
1 aa
1 bb
2 aaa
......
1. Q. What is a join?
A. Join is a process of retrieve pieces of data from different sets (tables) and returns them to the user or program as one joined collection of data.
2. Q. Can a table have more than one foreign key defined?
A. A table can have any number of foreig ......
写一个存储过程,将表一按照表二的形式进行查询。
仓库名称 商品名称 数量
A
S001 12
A S002 17
A
S003 10
B S001 21
B
S002 5
B S003 ......
从博客园中看到一篇文章,介绍大软件公司面试时常常会出的两道SQL题(见附录)。
我觉得受益很多,在此之前,我一直觉得,SQL2008似乎提供了这方面的支持,但更低的版本,包括2005,非游标做不出来(水平够菜)。总结心得如下:
1、 强大的group by
1 select stdname,
2 isnull(sum( ......