LIMIT results in MySQL?

2020-02-06 17:23发布

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.

  2. I want to check the event is active ('event_active') is Yes(the data in this field is Yes/No).

  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.

  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.

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.

标签: php mysql limit
4条回答
smile是对你的礼貌
2楼-- · 2020-02-06 17:39

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.

查看更多
Evening l夕情丶
3楼-- · 2020-02-06 17:45

I am really not sure what you are asking, but LIMIT works as follows:

The LIMIT means that after your query is done, and ALL WHERE statements are processed, only the first 5 are returned.

ALl results where event_active is not 'Yes' will not be shown, and disregarded in everything.

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.

查看更多
\"骚年 ilove
4楼-- · 2020-02-06 17:46
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
  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. The sort will stop after 5 lines, also means - it get faster
  3. The ASC in the ORDER BY command is not necessary, because ASC is default
查看更多
老娘就宠你
5楼-- · 2020-02-06 17:55

The problem, I think, is with your 'event_active'.

MySQL uses 0 and 1 to indicate whether the field is true/false, yes/no, on/off. 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.

查看更多
登录 后发表回答