简体   繁体   English

限制结果在 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.我想我可能需要一个子查询,但我在这种事情上并不那么先进,所以任何正确方向的帮助或指示都将不胜感激。

Here's the Query I have....这是我的查询....

$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):好的......现在我想要实现的简单英语位(以了解我想要实现的目标):

  1. 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_type ') 是我得到的(即圣诞节列表),因为此列中有多种类型。

  2. I want to check the event is active (' event_active ') is Yes(the data in this field is Yes/No).我想检查事件是否处于活动状态(' event_active ')是是(该字段中的数据是是/否)。

  3. 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.我想按 (' event_date ') ASC(此字段中的数据为yyyy-mm-dd )对它们进行排序,因此它们会按日期显示来自数据库的最新条目。

  4. 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.当通过 WHILE 语句运行此类查询时,我想限制(或以某种方式控制输出)结果只显示 5 个结果。

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?当我进入实际的输出显示时,我在实际显示的结果数量上有一个不稳定的输出......如果我有多个关闭的事件会发生什么,如event_active是“ Off ”那么它几乎就像参数是从所有结果(包括event_active='Off' )计算的,因此没有显示我希望它们如何显示?

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. You shoul use 1 / 0 instead of Yes / no你应该使用 1 / 0 而不是 Yes / no
  2. 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.第一步 - 执行包括 WHERE 在内的查询 第二步 - ORDER BY 第三步 - LIMIT 如果您在排序的列上设置了索引。 The sort will stop after 5 lines, also means - it get faster排序将在 5 行后停止,也意味着 - 它变得更快
  3. The ASC in the ORDER BY command is not necessary, because ASC is default ORDER BY 命令中的 ASC 不是必需的,因为 ASC 是默认的

I am really not sure what you are asking, but LIMIT works as follows:我真的不确定你在问什么,但LIMIT工作原理如下:

The LIMIT means that after your query is done, and ALL WHERE statements are processed, only the first 5 are returned. LIMIT表示在您的查询完成并处理所有WHERE语句后,仅返回前 5 个。

ALl results where event_active is not 'Yes' will not be shown, and disregarded in everything.所有的结果,其中event_active不是“是”将不会被显示,并在一切都忽略不计。

This result is the same as a result where you would do the query without the limit, and just look at the first 5 lines.此结果与您在没有限制的情况下进行查询的结果相同,只需查看前 5 行。

That query should be fine.那个查询应该没问题。 I'd check your data set (or even better, post it!).我会检查您的数据集(或者甚至更好,发布它!)。 You might want to look into normalizing the database too.您可能还想研究规范化数据库。 It'll help you out in the future.它会在未来帮助你。

The problem, I think, is with your 'event_active'.我认为问题在于您的“event_active”。

MySQL uses 0 and 1 to indicate whether the field is true/false, yes/no, on/off. MySQL 使用 0 和 1 来表示该字段是否为真/假、是/否、开/关。 Try using 0 and 1 in your SELECT statement unless the field type on that field is VARCHAR and you actually are using those words.尝试在 SELECT 语句中使用 0 和 1,除非该字段上的字段类型是 VARCHAR 并且您实际上正在使用这些词。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM