案例 多表查询

 

 

 

 

 

 


2.
SELECT
       t1.`id`, -- 员工编号
       t1.`ename`,-- 员工姓名
       t1.`salary`,
       t1.`mgr`,
       t3.`jname`,
       t3.`description`,
       t2.`loc`
       
FROM 
	emp t1,
	dept t2,
	job t3
WHERE 
	t1.`dept_id`=t2.`id`AND
	t1.`job_id`=t3.`id`;


3.
	
SELECT
        t1.`ename`,
        t1.`salary`,
        t2.`grade`
FROM
	emp t1,
	salarygrade t2
WHERE
	t1.`salary`BETWEEN t2.`losalary` AND t2.`hisalary`;



4.
SELECT
        t1.`ename`,
        t1.`salary`,
        t4.`jname`,
        t4.`description`,
        t3.`loc`,
        t2.`grade`
FROM
	emp t1,
	salarygrade t2,
	job t4,
	dept t3
WHERE
	t1.`salary`BETWEEN t2.`losalary` AND t2.`hisalary`
    AND
	t1.`dept_id`=t3.`id`
    AND
    t1.`job_id`=t4.`id`;



5.注意这里是嵌套查询
SELECT
	t1.`id`,
	t1.`dname`,
	t1.`loc`,
	t2.total
FROM
	dept t1,
	(SELECT 
             dept_id,COUNT(id) total
	FROM
	     emp
	GROUP BY dept_id) t2    分组查询
	         
WHERE
	t1.`id`=t2.dept_id;




6.注意这里是重名的表  
SELECT
	t1.`ename`,
	t2.`ename`
FROM 
	emp t1
LEFT JOIN
	emp t2
ON 
        t1.`mgr`=t2.`id`

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值