MySQL return 输出为空集
MySQL return output in empty set
我有 movie
作为我的 table
我尝试执行以下查询,其中包含 movie_genre='comedy'
和 movie_genre='family'
的 2 个条件:
select movie_title, movie_cost, movie_genre
from movie
where movie_genre ='comedy'
and movie_genre ='family'
order by movie_cost asc;
执行查询后 return Empty set
。
虽然我尝试了一个条件它正常工作但是当添加 2 个条件时它再次出错。
我该如何解决这个问题?
SELECT movie_title, movie_cost, movie_genre
FROM movie
WHERE
movie_genre ='comedy'
OR
movie_genre ='family'
ORDER BY movie_cost ASC;
你应该使用OR
因为AND
意味着两个条件必须同时为真,但那是不可能的!
不同的查询语法可以是
SELECT movie_title, movie_cost, movie_genre
FROM movie
WHERE movie_genre IN ('comedy', 'family')
ORDER BY movie_cost ASC;
您必须使用 OR
来检查 movie_genre
是否是两个值中的一个。
更新查询 -
select movie_title, movie_cost, movie_genre from movie
where movie_genre ='comedy' or movie_genre ='family'
order by movie_cost asc;
还有一个选项是 UNION ALL:
SELECT movie_title, movie_cost, movie_genre
FROM movie
WHERE movie_genre = 'comedy'
UNION ALL
SELECT movie_title, movie_cost, movie_genre
FROM movie
WHERE movie_genre = 'family'
ORDER BY movie_cost ASC;
我有 movie
作为我的 table
我尝试执行以下查询,其中包含 movie_genre='comedy'
和 movie_genre='family'
的 2 个条件:
select movie_title, movie_cost, movie_genre
from movie
where movie_genre ='comedy'
and movie_genre ='family'
order by movie_cost asc;
执行查询后 return Empty set
。
虽然我尝试了一个条件它正常工作但是当添加 2 个条件时它再次出错。
我该如何解决这个问题?
SELECT movie_title, movie_cost, movie_genre
FROM movie
WHERE
movie_genre ='comedy'
OR
movie_genre ='family'
ORDER BY movie_cost ASC;
你应该使用OR
因为AND
意味着两个条件必须同时为真,但那是不可能的!
不同的查询语法可以是
SELECT movie_title, movie_cost, movie_genre
FROM movie
WHERE movie_genre IN ('comedy', 'family')
ORDER BY movie_cost ASC;
您必须使用 OR
来检查 movie_genre
是否是两个值中的一个。
更新查询 -
select movie_title, movie_cost, movie_genre from movie
where movie_genre ='comedy' or movie_genre ='family'
order by movie_cost asc;
还有一个选项是 UNION ALL:
SELECT movie_title, movie_cost, movie_genre
FROM movie
WHERE movie_genre = 'comedy'
UNION ALL
SELECT movie_title, movie_cost, movie_genre
FROM movie
WHERE movie_genre = 'family'
ORDER BY movie_cost ASC;