码迷,mamicode.com
首页 > 数据库 > 详细

MySQL中关联查询相关语句

时间:2019-05-02 23:14:27      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:name   关联查询   rip   ade   code   dep   sele   select   rom   

-- emp和job查询
SELECT 
    t1.`id`,
    t1.`ename`,
    t1.`salary`,
    t2.`jname`,
    t2.`descriptions`
FROM 
    emp t1,job t2,dept t3
WHERE t1.`job_id`=t2.`id`AND t3.`id`=t1.`dept_id`;


-- 查询工资的等级
SELECT 
    t1.ename,t1.`salary`,t2.`grade`
FROM 
    emp t1,salarygrade t2
WHERE t1.`salary`BETWEEN t2.`hisalar`AND t2.`hisalar`;

SELECT 
    t1.`id`,t1.`dname`,t1.`loc`,t2.total
FROM
    dept t1,
    (SELECT dept_id,COUNT(id) total
    FROM 
        emp
    GROUP BY emp.`dept_id`) t2
WHERE t1.`id`=t2.dept_id;



SELECT
    t1.`ename`,
    t1.`mgr`,
    t2.`id`,
    t2.`ename`
FROM
    emp t1,emp t2
WHERE t1.`id`=t2.`mgr`;

SELECT 
t1.`ename`,
t1.`mgr`,
t2.`id`,
t2.`ename`
FROM emp t1
LEFT JOIN emp t2
ON t1.`mgr`=t2.`id`

 

MySQL中关联查询相关语句

标签:name   关联查询   rip   ade   code   dep   sele   select   rom   

原文地址:https://www.cnblogs.com/feixiangdecainiao/p/10803768.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!