mysql - 我怎样才能在where select中返回2行?

标签 mysql sql sql-server select where-clause

这是问题......我有这些表:

crs
title|semester
c++  |a
java |b
sql  |a

crsstu
regnum|title
11131 |java
11131 |c++
11132 |java
11132 |sql
11133 |c++
11133 |sql

我想要做的是从 crsstu 和表格中选择一个注册号(regnum),我想看看他还没有注册哪些类(class)。 示例:如果我选择regnum 11131,那么它必须返回sql,或者选择11132,它返回c++。

到目前为止我已经做到了:

select a.title 
from crs as a,crsstu as b
where b.registrationnumber != 11133 
and a.title != (select title 
                from crsstu 
                where registrationnumber = 11133)

但它向我显示了 1242 错误(子查询返回超过 1 行)。我知道我已经接近答案,但我不知道还要做什么。任何答案都会有帮助。提前谢谢您

最佳答案

只需将 != 更改为 NOT IN。 IN 和 NOT IN 允许对一组项目进行比较。

select a.title 
from crs as a,crsstu as b
where b.registrationnumber != 11133 
and a.title NOT IN (select title 
                from crsstu 
                where registrationnumber = 11133)

关于mysql - 我怎样才能在where select中返回2行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17279166/

相关文章:

python - 在包含 200 万个项目的列表中搜索项目 - Python

mysql - 使用 mysql 连接列

java - Jackson 无法序列化 Hibernate 实体

sql - 使用 SQL 身份验证时获取当前用户

mysql - 带有可选参数的命名查询在 mysql 中不起作用

python - 在 Python 中运行 MySQL 服务器

mysql 释放删除的表

php - MySQL ORDER BY 另一个表中用户的总行数

MySQL 合并表,值为零

c# - 防止多重请求 - WebAPI