第十二周_A-leetCode-SQL_销售员

53 阅读1分钟

题目连接:leetcode.cn/problems/sa…

这应该是最简单的了,直接查询出满足的 id,然后赋值给下一个 SQL 查询。按照只能利用 NOT IN

因为有些销售人员不是 RED 类的,但是也不在 Orders 表中,如果使用 IN ,必然会有漏网之鱼。

/**
*  https://leetcode.cn/problems/sales-person/description/
*
*
*/
public class 销售员 {
    //自己写的,直接套娃。
    String s="select name from SalesPerson where sales_id not in\n" +
        "(select sales_id from Orders where com_id = (\n" +
        "    select com_id from Company where name='RED'\n" +
        ") )\n";

    //利用关联查询解决
    String S="SELECT s.name AS name FROM SalesPerson s\n" +
        "WHERE s.sales_id NOT IN (\n" +
        "    SELECT o.sales_id FROM Orders o \n" +
        "        INNER JOIN Company c ON o.com_id = c.com_id\n" +
        "        WHERE c.name = 'RED'\n" +
        ");\n";
}