SQL怎么连接查询2个表?

如:表一员工申请表ygsq,字段:id,标题,内容

表二管理人员审核表 ygsq_jl,字段:id,ygsq_id,审核内容

使用where语句进行查询,如:

select Emp.E_Id,Company.C_OraName from Emp,Company where Companey.C_Id=Emp.C_Id

但是往往会碰到比较复杂的语句,这时候使用where就不太合适了,其实SQL可以用较为直接的形式进行连接操作,可以在From子句中以直接的形式指出:

select top 10 E_Id,E_Name,C_Name 

from 

Emp join Companey on Companey.C_Id=Emp.C_Id 

where 

E_Id not in (select top 20 E_Id from Emp order by  E_Id  asc) 

order by E_Id asc

//查询表Emp中第21到第30条数据以升序排列,其中C_Name来自于另一个表

扩展资料:

SQL查询语句

1、获取当前数据库中的所有用户表select Name from sysobjects where xtype='u' and status>=0

2、获取某一个表的所有字段select name from syscolumns where id=object_id('表名')select name from syscolumns where id in (select id from sysobjects where type = 'u' and name = '表名')

3、查看与某一个表相关的视图、存储过程、函数select a.* from sysobjects a, syscomments b where a.id = b.id and b.text like '%表名%'

4、查看当前数据库中所有存储过程select name as 存储过程名称 from sysobjects where xtype='P'

5、查询用户创建的所有数据库select * from master..sysdatabases D where sid not in(select sid from master..syslogins where name='sa')

或者select dbid, name AS DB_NAME from master..sysdatabases where sid <> 0x01

6、查询某一个表的字段和数据类型select column_name,data_type from information_schema.columnswhere table_name = '表名'

温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-09-24
select * from ygsq b1 inner join ygsq_jl b2 on b1.id = b2.ygsql_id 这样就把两张表的所有数据都查询出来了。楼主还想怎么查?如果单纯的连接查询就是这样。
第2个回答  2010-09-24
select XXX from ygsq a,ygsq_ji b where a.id=b.ygsq_id
第3个回答  2010-09-24
inner join用法:
select * from ygsq as a inner join ygsq_jl as b
on a.id=b.ygsq_id

left join用法:
select * from ygsq as a left join ygsq_jl as b
on a.id=b.ygsq_id

right join用法:
select * from ygsq as a right join ygsq_jl as b
on a.id=b.ygsq_id

where用法:
select * from ygsq as a,ygsq_jl as b
where a.id=b.ygsq_id

希望回答对你有帮助。本回答被提问者采纳
相似回答