当前位置:Gxlcms > 数据库问题 > sql语句

sql语句

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

多表查询

select empno,ename,job,sal,dept.deptno,dname,loc

    from emp,dept

    where emp.deptno(+)=dept.deptno; -- (Oracle 8i 及以前的写法)

    --另一种写法(右连接):     -- (SQL 99的写法)

    select empno,ename,job,sal,dept.deptno,dname,loc

    from emp right join dept on (emp.deptno=dept.deptno);

多条件查询

select * from TABLE where USERID=a or USERID= b or USERID=c

但是有一种效率更高的

 用in实现,比如
select * from TABLE where USERID in(a,b,c,d……)

或者普通的多列选项

SELECT * FROM T_Employee 
WHERE FNumber BETWEEN ‘DEV001‘ AND ‘DEV008‘ 
AND FName LIKE ‘%J%‘ 
AND FSalary BETWEEN 3000 AND 6000

多表多条件查询语句

UNION 操作符用于合并两个或多个 SELECT 语句的结果集。

请注意,UNION 内部的 SELECT 语句必须拥有相同数量的列。列也必须拥有相似的数据类型。同时,每条 SELECT 语句中的列的顺序必须相同。

例子

有5个结构相同的表,表1、表2、表3、表4、表5,对这5个表单条件查询Select * from 表1 where 入职日期=‘2014-04-20‘ union all Select * from 表2 where 入职日期=‘2014-04-20‘ union all..........union all Select * from 表5 where 入职日期=‘2014-04-20‘ 查询是成功的,如果再加上一个查询条件 ,岗位=普工,表达式该怎么写?

Select * from 表1 where 入职日期=‘2014-04-20‘ and 岗位=‘普工‘ 
union all
Select * from 表2 where 入职日期=‘2014-04-20‘ and 岗位=‘普工‘
union all
..........
union all
Select * from 表5 where 入职日期=‘2014-04-20 and 岗位=‘普工‘

或者

select * from
(select * from 表bai1
union all
select * from 表2
union all
select * from 表3
union all
select * from 表4
union all
select * from 表5)tb
where 入职日du期=‘2014-04-20‘ and 岗位zhi=‘普工‘

 

sql语句

标签:sele   判断   join   遇到   多少   条件   效率   表达式   顺序   

人气教程排行