左向外连接,右向外连接,全连接的关键字:
SQL1999新语法
Left Join On 或 Left Outer Join On
Right Join On 或 Right Outer Join On
Full Join On 或 Full Outer Join On
Inner Join的意思、作用、语法结构:
内连接又称相等连接、等值连接。
他返回两表的列,但只返回连接列中具有相等值的行。
select * from t1 inner join t2 on t1.id=t2.id;
select * from t1 t1,t2 t2 where t1.id=t2.id;
某字段有10条重复记录请把对应表中的记录查询出来:
表名:appeal_base_info
字段:appeal_type(number)
select * from appeal_base_info where appeal_type in(select appeal_type from appeal_base_info group by appeal_type having count(appeal_type)>=10);
一张主表,一张从表,请将两张表的所有记录查询出来:
主表:appeal_base_info 主键:base_id(number)
从表:appeal_person_info 主键:person_id(number) 外键:base_id(number)
select * from appeal_base_info as bfull join appeal_person_info as p on b.base_id=p.person_id;
已知原表(t_salary)
year salary
2000 1000
2001 2000
2002 3000
2003 4000
先要实现显示结果(salary为以前的工资和)
year salary
2000 1000
2001 3000
2002 6000
SQL语句:
select year, (select sum(salary) from t_salary t2 where t2.year<=t1.year) salary
from t_salary t1
group by year;
两张关联表,删除主表中已经存在附表中没有的信息:
delete from info where not exists (select * from infobz where info.infid=infobz.infid);
有两个表A和B,均有key和value两个字段,如果B的key在A中也有,就把B的value换为A的对应value,SQL:
update b set b.value=(select a.value from a where a.key = b.key) where b.id in(select b.id from a,b where b.key=a.key);
查询订单表里F_Name以abc开头的所有订单,其中显示列为 订单名称、订单价格、订单时间,根据订单ID降序排列:
select F_Name,F_Price,F_Date from F where F_Name like 'abc%' order by id desc;
根据分页查询,一页10条记录,查询第25页的记录数:
(n:一页多少条;m:显示第几页)
SQL SERVER:
select top n *
from t
where id not in(select top n * m id from t order by id) order by id;
ORACLE:
select t2.*
from (select t1.* from (select rownum rn,t.* from company t) t1 where t1.rn < (n * (m + 1))) t2
where t2.rn > (n * m);
MYSQL:
select * from t limit (n * m), (n * (m + 1));
取出表A中第31到第40记录(SLQSERVER,以自增长的ID作为主键,注意:ID可能不是连续的):
select top 10 * from t where id not in(select top 30 id from t order by id) order by id;
select top 10 * from t where id not in(select top 30 id from t order by id);
创建一个表users,表结构式Id,username,datetimes,并查询datetimes是2008-4-1 和 2002-4-28的username:
create table users(id int identity(1,1) primary key, username varchar(30) not null, dattetimes datetime not null default(getdate()));
select username from users where datetimes between '2002-4-28' and '2008-4-1';
在A表中(A表与B表相关联或者相同)查询出B表与A表没有重复的数据:
select * from b where b01 not exsits(select a01 from a)
表中有A、B、C三列,用SQL语句实现:当A列大于B列时候选择A列,否则选择B列;当B列大于C列时选择B列,否则选择C列:
select (case when a>b then a else b end),(case when b>c then b else c end)
from table_name;
一个表中的ID有多个记录,把所有这个ID的记录查出来,并显示共有多少条记录数:
select id,count(id) from tb group by id having count(id) > 1;
select * from (select count(id) cnt from tb group by id) t where t.cnt > 1;
一简单SQL题
s(sno,sname) 学生表
c(cno,cname,cteacher) 课程表
sc(sno,cno,scgrade) 成绩表
1、没有选“李明”老师课程的所有学生
SELECT s.*
FROM s
WHERE s.sno NOT IN
(
SELECT sc.sno
FROM sc
JOIN c
ON sc.cno = c.cno
WHERE
c.cno =
(
SELECT cno
FROM c
WHERE cname = '李明'
)
);
2、列出两门及两门课程以上不及格学生的姓名及平均成绩
SELECT s.sname,t.avgs
FROM s
JOIN
(
SELECT sno
FROM sc
WHERE scgrade < 60
GROUP BY sno
HAVING count(1) >= 2
) t1
ON t1.sno = s.sno
JOIN
(
SELECT sno, avg(scgrade) avgs
FROM sc
GROUP BY sno
) t
ON t.sno = t1.sno;
3、学过1号课程又学过2号课程的所有学生的姓名
方法一:
SELECT s.sname
FROM s
JOIN
(
SELECT sno
FROM SC
JOIN
(
SELECT sno
FROM sc
WHERE cno = 2
) t
ON t.sno = sno
WHERE cno = 1
) t1
ON t1.sno = s.sno;
方法二:
SELECT sname
FROM s
WHERE
sno IN
(
SELECT sno
FROM sc
WHEREcno = 1
AND
sno IN
(
SELECT sno
FROM sc
WHERE cno = 2
)
)