LIMIT结果在MySQL中? [英] LIMIT results in MySQL?
问题描述
好;这已经让我的大脑煎熬了好几个小时了.我想我可能需要一个子查询,但是在这种情况下我没有那么先进,因此任何对正确方向的帮助或指点都将不胜感激.
ok; this has been frying my brain for hours. I think I might need a sub query, but I'm not that advanced at this kind of thing so any help or pointers in the right direction would be greatly appreciated.
这是我的查询....
$query = "SELECT * FROM events WHERE event_type = 'Christmas Listings' AND event_active='Yes' ORDER BY event_date ASC LIMIT 5";
$result= mysql_query($query);
好...现在,用普通的英语点讲我要实现的目标(以了解我要实现的目标):
OK... now for the plain english bit on what I want to achieve (to understand what I'm trying to achieve):
-
我想检查事件类型('
event_type
')是我得到的(即圣诞节清单),因为此列中有多种类型.
I want to check the event type ('
event_type
') is what I'm getting (ie. Christmas Listings) as there are multiple types in this column.
我要检查事件是否处于活动状态('event_active
')为是(此字段中的数据为是/否).
I want to check the event is active ('event_active
') is Yes(the data in this field is Yes/No).
我想按('event_date
')ASC排序(此字段中的数据为yyyy-mm-dd
),以便它们按日期从数据库中显示最新条目.
I want to order them by the ('event_date
') ASC (the data in this field is yyyy-mm-dd
) so they show the latest entry by its date from the DB.
我希望通过WHILE语句运行这种查询时,将结果限制为(或以某种方式控制输出)仅显示5个结果.
I want to LIMIT (or in some way control the output) the results to only have 5 results displayed when running this kind of query through a WHILE statement.
好的,但是所有这些都可以;当我到达实际输出显示时,我实际输出的结果显示摇晃的输出...发生的情况是,如果我关闭了多个事件,如event_active
为'Off
',那么几乎就像该参数从所有结果(包括event_active='Off'
)开始计数,因此没有显示我希望它们显示的方式吗?
OK, this all works BUT; when I get to the actual output display, i'm having a shaky output in how many results are actually display... What happens is if I have multiple events which are switched off, as in event_active
is 'Off
' then its almost like the argument is counting from the all the results that are (including event_active='Off'
) and consequently not showing how I expect them to display?
希望这是有道理的....我们将不胜感激.
Hope this makes sense.... Any help would be gratefully received.
推荐答案
SELECT *
FROM events
WHERE event_type = 'Christmas Listings' AND event_active='Yes'
ORDER BY event_date
LIMIT 0, 5
因此您的陈述更易于阅读.
so your statement is easyer to read..
- 您应使用1/0代替是/否
- 限制不包括所有行! 第一步-进行查询,包括WHERE 第二步-ORDER BY 第三步-LIMIT 如果您在列上设置了索引,则可以对其进行排序.排序将在5行后停止, 也意味着-它变得更快
- 因为默认为ASC,所以不需要ORDER BY命令中的ASC
- You shoul use 1 / 0 instead of Yes / no
- The Limit does not count all lines! First step - doing the query including WHERE Second step - ORDER BY Third step - LIMIT If you have set an index on the colum you sort. The sort will stop after 5 lines, also means - it get faster
- The ASC in the ORDER BY command is not necessary, because ASC is default
这篇关于LIMIT结果在MySQL中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!