SELECT c.name FROM customer as c WHERE referee_id != 2 OR referee_id is null
注意:MySQL 使用三值逻辑 —— TRUE, FALSE 和 UNKNOWN。任何与 NULL 值进行的比较都会与第三种值 UNKNOWN 做比较。这个“任何值”包括 NULL 本身!这就是为什么 MySQL 提供 IS NULL 和 IS NOT NULL两种操作来对 NULL 特殊判断。因此,在 WHERE 语句中我们需要做一个额外的条件判断 referee_id IS NULL
方法二:
1 2 3
select name from customer where ifnull(referee_id,0)!=2;
SELECT sell_date, COUNT(distinct product) AS num_sold, #不同的(distinct product)按照字典排序(order by product)然后按照“,”分割(separator ',') GROUP_CONCAT(distinct product ORDER BY product separator ',') AS products FROM Activities GROUP BY sell_date ORDER BY sell_date;
select employee_id from Employees where employee_id not in( select employee_id from Salaries )
union
select employee_id from Salaries where employee_id not in( select employee_id from Employees )
order by employee_id
方法二:
1 2 3 4 5 6 7 8 9
select employee_id from ( select employee_id from Employees union all select employee_id from Salaries ) temp group by 1 having count(1)=1 order by employee_id
创建临时表
1 2 3 4 5 6 7 8 9 10
with temp as( select employee_id from Employees union all select employee_id from Salaries ) select temp.employee_id from temp group by 1 having count(1)=1 order by employee_id
# Write your MySQL query statement below select product_id, 'store1' as store, store1 as price from Products where store1 IS NOT NULL union all select product_id, 'store2' as store, store2 as price from Products where store2 IS NOT NULL union all select product_id, 'store3' as store, store3 as price from Products where store3 IS NOT NULL
# 根节点:无父节点 select id,'Root' as type from tree where p_id is null
union all
# 内部节点:有父节点,有子节点 select id,'Inner' as type from tree where id in( select distinct p_id from tree where p_id is not null ) and p_id is not null
union all
# 叶子节点:有父节点,无子节点 select id,'Leaf' as type from tree where id not in( select distinct p_id from tree where p_id is not null ) and p_id is not null
order by id
方法二:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
select id, case when tree.id = ( select atree.id from tree atree where atree.p_id is null ) then 'Root' when tree.id in( select atree.p_id from tree atree ) then 'Inner' else 'Leaf' end as type from tree order by id
select customer_id,count(visit_id) as count_no_trans from Visits where visit_id not in( select visit_id from Transactions ) group by customer_id
方法二:
1 2 3 4 5 6
select v.customer_id,count(v.customer_id) - count(t.visit_id) as count_no_trans from Visits as v left join Transactions as t on v.visit_id = t.visit_id group by v.customer_id having count_no_trans != 0
# 子查询 select name from SalesPerson where sales_id not in( select sales_id from Orders where com_id in( select com_id from Company where name = 'RED' ) )
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT BEGIN set N = N - 1; RETURN ( # Write your MySQL query statement below. select ifNULL(( select distinct Salary from Employee order by Salary desc limit N,1),NULL) as getNthHighestSalary ); END
方法二:单表查询
1 2 3 4 5 6 7 8 9 10 11 12
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT BEGIN set N := N - 1; RETURN ( # Write your MySQL query statement below. select salary from Employee group by salary order by salary desc limit N,1 ); END
select activity_date as 'day', count(distinct user_id) as 'active_users' from activity where dateDiff('2019-07-27',activity_date) < 30 and activity_date < '2019-07-27' group by activity_date
select date_id, make_name, count(distinct lead_id) as unique_leads, count(distinct partner_id) as unique_partners from DailySales group by date_id, make_name
select d.name as Department, e.name as Employee, e.salary as Salary from Employee e left join Department d on e.departmentId = d.id where( e.DepartmentId, Salary )in( select DepartmentId, MAX(Salary) from Employee group by DepartmentId )