题目说明
练习题目来自牛客网sq在线编程 非技术快速入门高级操作符练习(2)_牛客题霸_牛客网 (nowcoder.com)
测试用例
create database nk_test; //创建数据库
use nk_test; //进入数据库
-- 创建测试表
drop table if exists user_profile;
CREATE TABLE `user_profile` (
`id` int,
`device_id` int,
`gender` varchar(14),
`age` int,
`university` STRING,
`province` STRING,
`gpa` float);
-- 向测试表中插入数据
INSERT INTO user_profile VALUES(1,2138,'male',21,'北京大学','BeiJing',3.4);
INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学','Shanghai',4.0);
INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学','BeiJing',3.2);
INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学','ZheJiang',3.6);
INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学','Shandong',3.8);
描述
题目:现在运营想要找到学校为北大或GPA在3.7以上(不包括3.7)的用户进行调研,请你取出相关数据(使用OR实现)
示例:user_profile
id | device_id | gender | age | university | gpa |
---|---|---|---|---|---|
1 | 2138 | male | 21 | 北京大学 | 3.4 |
2 | 3214 | male | 复旦大学 | 4.0 | |
3 | 6543 | female | 20 | 北京大学 | 3.2 |
4 | 2315 | female | 23 | 浙江大学 | 3.6 |
5 | 5432 | male | 25 | 山东大学 | 3.8 |
根据输入,你的查询应返回以下结果:
device_id | gender | age | university | gpa |
---|---|---|---|---|
2138 | male | 21 | 北京大学 | 3.4 |
3214 | male | 复旦大学 | 4.0 | |
6543 | female | 20 | 北京大学 | 3.2 |
5432 | male | 25 | 山东大学 | 3.8 |
解决方案
SELECT device_id,gender,age,university,gpa
FROM user_profile
WHERE (university="北京大学" or gpa>3.7);
方案分析
题目中有两个筛选条件,条件一:学校为北大,条件二gpa在3.7以上,两个条件关系是或 应使用 or 连接。