MySQL不正确的输出不知道什么是错误的查询
试图编写和执行一个MySQL查询来显示电影号码,电影标题,电影成本和电影流派的所有电影是行动或喜剧电影,并有一个成本低于50美元。按类型对结果按升序排序。MySQL不正确的输出不知道什么是错误的查询
mysql> select mv_no, mv_name, mv_cost, mv_genre from movie
-> where mv_genre = ('Action' or 'Comedy') and mv_cost < 50
-> order by mv_genre;
+-------+----------------------------+---------+----------+
| mv_no | mv_name | mv_cost | mv_genre |
+-------+----------------------------+---------+----------+
| 1245 | Time to Burn | 45.49 | ACTION |
| 1237 | Beatnik Fever | 29.95 | COMEDY |
| 1239 | Where Hope Dies | 25.49 | DRAMA |
| 1234 | The Cesar Family Christmas | 39.95 | FAMILY |
+-------+----------------------------+---------+----------+
4 rows in set, 10 warnings (0.00 sec)
假如你试图使用IN
条款:
select mv_no, mv_name, mv_cost, mv_genre
from movie
where mv_genre in ('Action', 'Comedy')
and mv_cost < 50
或者你可以改变你使用的是OR
条款的方式:
select mv_no, mv_name, mv_cost, mv_genre
from movie
where (mv_genre = 'Action' or mv_genre= 'Comedy')
and mv_cost < 50
这就是我要推荐一下。在原文中使用'OR'是错误的。 – 2013-04-08 15:55:45
变化到:
select mv_no, mv_name, mv_cost, mv_genre from movie
where (mv_genre = 'Action' or mv_genre = 'Comedy') and mv_cost < 50
order by mv_genre;
像魅力一样工作,谢谢先生。 – Daniel 2013-04-08 15:59:26
的错误似乎是你的磨损子句中,你可以使用一个IN
,如:
WHERE mv_genre IN ('Action', 'Comedy')
您当前的mv_genre = ('Action' or 'Comedy')
条款不计算到if the genre is 'Action' or the genre is 'Comedy'
它实际上只是意味着你要评估是否'Action' OR 'Comedy'
是真的,然后比较结果mv_genre
这将永远不会返回您所期望的。您可以轻松地使用IN
多个值,然后比较列表或者你能更详细和比较喜欢:
WHERE (mv_genre = 'Action' OR mv_genre = 'Comedy')
无论你喜欢和适合您的需要。
您的查询应该是
mysql> select mv_no, mv_name, mv_cost, mv_genre from movie
-> where mv_genre IN ('Action', 'Comedy') and mv_cost < 50
-> order by mv_genre;
它不是想表现出戏剧或电影家庭 – Daniel 2013-04-08 15:57:49