1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > SQL9 查找除复旦大学的用户信息(数据库否定语句写法)

SQL9 查找除复旦大学的用户信息(数据库否定语句写法)

时间:2022-08-04 00:11:42

相关推荐

SQL9 查找除复旦大学的用户信息(数据库否定语句写法)

描述

题目:现在运营想要查看除复旦大学以外的所有用户明细,请你取出相应数据

示例:user_profile

根据输入,你的查询应返回以下结果:

示例1

输入:

drop table if exists user_profile;CREATE TABLE `user_profile` (`id` int NOT NULL,`device_id` int NOT NULL,`gender` varchar(14) NOT NULL,`age` int ,`university` varchar(32) NOT NULL,`province` varchar(32) NOT NULL);INSERT INTO user_profile VALUES(1,2138,'male',21,'北京大学','BeiJing');INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学','Shanghai');INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学','BeiJing');INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学','ZheJiang');INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学','Shandong');

输出:

2138|male|21|北京大学6543|female|20|北京大学2315|female|23|浙江大学5432|male|25|山东大学

AC代码:

几种方法:

1、直接where条件语句用 !=

select device_id,gender,age,universityfrom user_profilewhere university!="复旦大学";

2、直接where条件语句用 not in()

select device_id,gender,age,universityfrom user_profilewhere university not in ("复旦大学");

语法举例

1)查询 city 表中 ID 不是在10~100之间的所有值。

SELECT IDFROM cityWHERE ID NOT IN(SELECT IDFROM cityWHERE ID > 11 AND ID < 100);

2)查询 city 表中 ID 除 10 和 100 之外的所有值。

SELECT IDFROM cityWHERE ID NOT IN(10,100);

3、<> 运算符

作用:表示不等于。

说明:和 “!=” 运算符的作用一致,相较之下 “<>” 的可读性较差。

select device_id,gender,age,university from user_profile where university <> '复旦大学';

4、not 运算符:否定它之后所跟的任何条件。

注意和第二种not in 之间的区别

SELECT `device_id`,`gender`,`age`,`university` from user_profile where not (university = "复旦大学")

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。