本文介绍: 超过经理收入员工显然是要将同一张表,作为经理员工连接这里存在两种方法,一种是采用WHERE。居然不能selectupdate,还要通过一个中间表去解决本题的关键在于多表连接,三表之间的连接与两表是一致的。对于前者直接采用distinct关键字,而后者可以本题的关键点在于过滤重复的以及null处理。最先想到的方法是找到所有订购过的,然后排除。另一种巧妙的方法是左连接筛选。另一种是使用JOIN。

超过经理收入员工

超过经理收入员工显然是要将同一张表,作为经理和员工连接这里存在两种方法,一种是采用WHERE

SELECT
    a.Name AS 'Employee'
FROM
    Employee AS a,
    Employee AS b
WHERE
    a.ManagerId = b.Id
        AND a.Salary > b.Salary

另一种是使用JOIN

SELECT
     a.NAME AS Employee
FROM Employee AS a JOIN Employee AS b
     ON a.ManagerId = b.Id
     AND a.Salary > b.Salary

https://leetcode.cn/problems/employeesearning-morethantheirmanagers/

从不订购的客户

最先想到的方法是找到所有订购过的,然后排除

select customers.name as 'Customers'
from customers
where customers.id not in
(
    select customerid from orders
)

另一种巧妙的方法是左连接筛选

SELECT name AS 'Customers'
FROM Customers
LEFT JOIN Orders ON Customers.Id = Orders.CustomerId
WHERE Orders.CustomerId IS NULL

https://leetcode.cn/problems/customers-who-neverorder/description/

删除重复电子邮箱

DELETE p1 FROM Person p1,
    Person p2
WHERE
    p1.Email = p2.Email AND p1.Id > p2.Id

居然不能selectupdate,还要通过一个中间表去解决

https://blog.csdn.net/fdipzone/article/details/52695371

https://leetcode.cn/problems/deleteduplicateemails/description/

销售

本题的关键在于多表连接,三表之间的连接与两表是一致的

SELECT
    s.name
FROM
    salesperson s
WHERE
    s.sales_id NOT IN (SELECT
            o.sales_id
        FROM
            orders o
                LEFT JOIN
            company c ON o.com_id = c.com_id
        WHERE
            c.name = 'RED')

https://leetcode.cn/problems/salesperson/description/

第二高的薪水

本题的关键点在于过滤重复的以及null处理

对于前者直接采用distinct关键字,而后者可以

SELECT
    IFNULL(
      (SELECT DISTINCT Salary
       FROM Employee
       ORDER BY Salary DESC
        LIMIT 1 OFFSET 1),
    NULL) AS SecondHighestSalary

https://leetcode.cn/problems/secondhighest-salary/description/

原文地址:https://blog.csdn.net/iUcool/article/details/134754153

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_28536.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注