MySQL 练习<1>

MySQL 练习

大家好呀,我是小笙,今天我来分享一些 Leetcode 上的MySQL的练习

1757. 可回收且低脂的产品

写出 SQL 语句,查找既是低脂又是可回收的产品编号。

# 创建表
Create table If Not Exists Products 
(
    product_id int, 
    low_fats ENUM('Y', 'N'), 
    recyclable ENUM('Y','N')
)
-- product_id 是这个表的主键。
-- low_fats 是枚举类型,取值为以下两种 ('Y', 'N'),其中 'Y' 表示该产品是低脂产品,'N' 表示不是低脂产品。
-- recyclable 是枚举类型,取值为以下两种 ('Y', 'N'),其中 'Y' 表示该产品可回收,而 'N' 表示不可回收。

# 插入数据
insert into Products (product_id, low_fats, recyclable) values ('0', 'Y', 'N')
insert into Products (product_id, low_fats, recyclable) values ('1', 'Y', 'Y')
insert into Products (product_id, low_fats, recyclable) values ('2', 'N', 'Y')
insert into Products (product_id, low_fats, recyclable) values ('3', 'Y', 'Y')
insert into Products (product_id, low_fats, recyclable) values ('4', 'N', 'N')

示例

Products 表:
+-------------+----------+------------+
| product_id  | low_fats | recyclable |
+-------------+----------+------------+
| 0           | Y        | N          |
| 1           | Y        | Y          |
| 2           | N        | Y          |
| 3           | Y        | Y          |
| 4           | N        | N          |
+-------------+----------+------------+
Result 表:
+-------------+
| product_id  |
+-------------+
| 1           |
| 3           |
+-------------+
只有产品 id 为 13 的产品,既是低脂又是可回收的产品

代码示例

select product_id from Products
            where low_fats = 'Y' AND recyclable = 'Y';

584. 寻找用户推荐人

写一个查询语句,返回一个客户列表,列表中客户的推荐人的编号都不是 2

# 创建表
Create table If Not Exists Customer 
(
    id int, 
    name varchar(25), 
    referee_id int
)

# 插入数据
insert into Customer (id, name, referee_id) values ('1', 'Will', 'None')
insert into Customer (id, name, referee_id) values ('2', 'Jane', 'None')
insert into Customer (id, name, referee_id) values ('3', 'Alex', '2')
insert into Customer (id, name, referee_id) values ('4', 'Bill', 'None')
insert into Customer (id, name, referee_id) values ('5', 'Zack', '1')
insert into Customer (id, name, referee_id) values ('6', 'Mark', '2')

示例

+------+------+-----------+
| id   | name | referee_id|
+------+------+-----------+
|    1 | Will |      NULL |
|    2 | Jane |      NULL |
|    3 | Alex |         2 |
|    4 | Bill |      NULL |
|    5 | Zack |         1 |
|    6 | Mark |         2 |
+------+------+-----------+
// 结果
+------+
| name |
+------+
| Will |
| Jane |
| Bill |
| Zack |
+------+

代码示例

# 因为查询结果只会包含 WHERE 子句里的判断结果为 true 的行,不包含判断结果为 false 和 unknown 的行
select name from customer
        where referee_id != 2 or referee_id is null;

196. 删除重复的电子邮箱

编写一个SQL查询来 删除 所有重复的电子邮件,只保留一个id最小的唯一电子邮件

# 创建表
Create table If Not Exists Person (
    Id int, 
    Email varchar(255)
)

# 插入数据
insert into Person (id, email) values ('1', 'john@example.com')
insert into Person (id, email) values ('2', 'bob@example.com')
insert into Person (id, email) values ('3', 'john@example.com')

示例 :

输入: 
Person 表:
+----+------------------+
| id | email            |
+----+------------------+
| 1  | john@example.com |
| 2  | bob@example.com  |
| 3  | john@example.com |
+----+------------------+
输出: 
+----+------------------+
| id | email            |
+----+------------------+
| 1  | john@example.com |
| 2  | bob@example.com  |
+----+------------------+
解释: john@example.com重复两次。我们保留最小的Id = 1

代码实现

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

1527. 患某种疾病的患者

写一条 SQL 语句,查询患有 I 类糖尿病的患者 ID (patient_id)、患者姓名(patient_name)以及其患有的所有疾病代码(conditions)。I 类糖尿病的代码总是包含前缀 DIAB1

# 创建表
Create table If Not Exists Patients (
    patient_id int, 
    patient_name varchar(30), 
    conditions varchar(100)
)

# 插入数据
insert into Patients (patient_id, patient_name, conditions) values ('1', 'Daniel', 'YFEV COUGH')
insert into Patients (patient_id, patient_name, conditions) values ('2', 'Alice', '')
insert into Patients (patient_id, patient_name, conditions) values ('3', 'Bob', 'DIAB100 MYOP')
insert into Patients (patient_id, patient_name, conditions) values ('4', 'George', 'ACNE DIAB100')
insert into Patients (patient_id, patient_name, conditions) values ('5', 'Alain', 'DIAB201')

示例:

输入:
Patients表:
+------------+--------------+--------------+
| patient_id | patient_name | conditions   |
+------------+--------------+--------------+
| 1          | Daniel       | YFEV COUGH   |
| 2          | Alice        |              |
| 3          | Bob          | DIAB100 MYOP |
| 4          | George       | ACNE DIAB100 |
| 5          | Alain        | DIAB201      |
+------------+--------------+--------------+
输出:
+------------+--------------+--------------+
| patient_id | patient_name | conditions   |
+------------+--------------+--------------+
| 3          | Bob          | DIAB100 MYOP |
| 4          | George       | ACNE DIAB100 | 
+------------+--------------+--------------+
解释:Bob 和 George 都患有代码以 DIAB1 开头的疾病。

代码实现

select
    patient_id,
    patient_name,
    conditions
from 
    Patients
where
    conditions like "DIAB1%" or  conditions like "% DIAB1%"

1873. 计算特殊奖金

写出一个SQL 查询语句,计算每个雇员的奖金。如果一个雇员的id是奇数并且他的名字不是以’M’开头,那么他的奖金是他工资的100%,否则奖金为0。返回的结果集请按照employee_id排序

# 创建表
Create table If Not Exists Employees (
    employee_id int, 
    name varchar(30), 
    salary int
)

# 插入数据
insert into Employees (employee_id, name, salary) values ('2', 'Meir', '3000')
insert into Employees (employee_id, name, salary) values ('3', 'Michael', '3800')
insert into Employees (employee_id, name, salary) values ('7', 'Addilyn', '7400')
insert into Employees (employee_id, name, salary) values ('8', 'Juan', '6100')
insert into Employees (employee_id, name, salary) values ('9', 'Kannon', '7700')

示例:

输入:
Employees 表:
+-------------+---------+--------+
| employee_id | name    | salary |
+-------------+---------+--------+
| 2           | Meir    | 3000   |
| 3           | Michael | 3800   |
| 7           | Addilyn | 7400   |
| 8           | Juan    | 6100   |
| 9           | Kannon  | 7700   |
+-------------+---------+--------+
输出:
+-------------+-------+
| employee_id | bonus |
+-------------+-------+
| 2           | 0     |
| 3           | 0     |
| 7           | 7400  |
| 8           | 0     |
| 9           | 7700  |
+-------------+-------+
解释:
因为雇员id是偶数,所以雇员id 是2和8的两个雇员得到的奖金是0
雇员id为3的因为他的名字以'M'开头,所以,奖金是0
其他的雇员得到了百分之百的奖金

代码实现

select 
    employee_id,if(employee_id % 2 = 1 and not(name like 'M%'),salary,0) as bonus 
from 
    Employees
order by 
    employee_id;

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
My练习是黑马程序员提供的一套用于学习和练习MySQL数据库的教程或课程。MySQL是一种开源的关系型数据库管理系统,被广泛应用于Web应用程序的开发和数据存储。通过参与MySql练习,学员可以掌握MySQL数据库的基本概念、语法和操作技巧,从而提升数据库管理和应用开发的能力。在学习过程中,学员将学习如何创建和管理数据库、表格、索引、以及使用SQL语言进行数据查询、修改和删除等操作。这些技能对于从事软件开发、数据库管理和数据分析等工作的人员来说都非常重要。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [瑞吉外卖+黑马程序员 Redis入门到实战(基础篇)](https://blog.csdn.net/m0_67184231/article/details/130262524)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [尚硅谷+黑马程序员MongoDB视频学习笔记(一)](https://blog.csdn.net/ok_wolf/article/details/106535777)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Al_tair

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值