Question
select name
from customer
where referee_id is NULL OR referee_id <> 2;
Note:
select customer_number
from Orders
Group by customer_number
having count(order_number) = (
select max(sum1)
from (
select customer_number, count(order_number) as sum1
from Orders
group by customer_number
)tmp
);
【Note】group by的相关知识点:
group by:分组查询,一般和聚合函数配合使用。
【使用原则】select 后面的列中,没有使用聚合函数的列,必须出现在 group by 后面!!
【注意事项】
方法1:自链接
select a.name as Employee
from Employee as a, Employee as b
where a.Salary > b.Salary and a.managerId=b.id;
方法2:子链接
select e.name as Employee
from Employee as e
where Salary>(select Salary from Employee where id=e.managerId);
Question
我的想法是建立一个自连接的表,配合子查询进行操作
select c.Num as ConsecutiveNums
from (
select l.Num, count(l.Num) as couNum
from Logs l, Logs l1
where (l.id = l1.id-1 OR l.id = l1.id-2) AND (l.num = l1.num)
) c
where c.couNum >= 3;
但问题就出现在,当出现两个连续数字的时候,会满足where的条件然后返回。
于是看了官方的解答,明白了可以使用三个表的自连接,配合distinct使用
select distinct l.Num as ConsecutiveNums
from Logs l, Logs l1, Logs l2
where
l.id = l1.id-1
and l1.id = l2.id-1
and l.num = l1.num
and l1.num = l2.num
目前对运行速度还没有改进要求,之后再来填坑。
最基础的题目,几乎是我在学习SQL之后第一道写的题目。但是发现自己好像每次都用自连接来解决问题,导致真的会很慢:
SELECT distinct p1.Email as Email
FROM Person p1, Person p2
WHERE p1.Id
查看评论区后整理:
方法1:自连接
方法2:having+count聚合
select Email from Person group by Email having count(Email) > 1;
方法3:子查询
select Email from (select count(1) as t, Email from Person group by Email) r where r.t > 1;
具体哪个快呢?
1略快于2,快于3
编写一个SQL查询,报告2019年春季才售出的产品。即仅在2019-01-01至2019-03-31(含)之间出售的商品。
# My code
select z.product_id, z.product_name
from (
select p.product_id, p.product_name, max(s.sale_date) max_d, min(s.sale_date) min_d
from Product p , Sales s
where p.product_id = s.product_id
group by p.product_id) z
where DATE(z.max_d) <= '2019-03-31' AND DATE(z.min_d)>='2019-01-01';
# from comment
select p.product_id,p.product_name
from product p right join sales s
on p.product_id=s.product_id
group by p.product_id
having min(sale_date)>='2019-01-01' and max(sale_date)<='2019-03-31';