Selecting different rows from database each calendar day [duplicate] - php

This question already has answers here:
MySQL: Alternatives to ORDER BY RAND()
(9 answers)
Closed 6 years ago.
I have a MySQL database with 112 rows (with ID column from 1 to 112), I need to SELECT 6 random rows (doesn't matter if sequential) to show in PHP/HTML page and to be changed daily.
The only option I think is to depend on the current date.
Is there any solution ?!
Thank you ...
EDIT:
Question was solved. Although no one really understood what I want but I keep getting down votes.
The question was : 6 Random rows EVERY DAY not every page refresh or every call from DB.
But thanks for the amazing effort. Question solved.

The MySQL statement ORDER BY RAND() will order the matching rows randomly. Combined with LIMIT 6 you'll get six random rows.
See http://dev.mysql.com/doc/refman/5.7/en/mathematical-functions.html#function_rand for reference.

Just use ORDER BY RAND() to randomize the row order, then show the first 6:
SELECT * FROM Yourtable ORDER BY RAND() LIMIT 0,6;

Use following query
SELECT column FROM table
ORDER BY RAND()
LIMIT 6

Related

How to fetch new records by using limit in php mysql? [duplicate]

This question already has answers here:
Pagination using MySQL LIMIT, OFFSET
(4 answers)
Closed 4 years ago.
I am making an infinite scroll app in which records from database will be fetch. When the user clicks "Load More" button a function is called for retrieving data from database.
My problem is that when I am running this(below) query every Time I am getting the same records.
SELECT ArticleTitle, PreviewText, PreviewImage FROM Articles ORDER BY ID DESC LIMIT 4
regards....
You need to set a offset in hidden field.
On clicking load more, you need to get offset, calculate next batch and update offset in same hidden field
that new offset, should be passed to query.
your query looks like
SELECT ArticleTitle, PreviewText, PreviewImage
FROM Articles
ORDER BY ID DESC
LIMIT 0, 4
first time offset is 0
next time it will be 5 (if you are loading 5 records every time)
then next time it will be 10 and so on
You've to use the OFFSET and LIMIT together like this.
Explanation: The OFFSET value is also most often used together with the LIMIT keyword. The OFF SET value allows us to specify which row to start from retrieving data. By the way you can also omit the OFFSET keyword and use like this LIMIT 0,4 which tells to get 4 rows starting from 0. see more
SELECT ArticleTitle, PreviewText, PreviewImage
FROM Articles
ORDER BY ID DESC LIMIT 4 OFFSET 0
SELECT ArticleTitle, PreviewText, PreviewImage
FROM Articles
ORDER BY ID DESC LIMIT 4 OFFSET 5

How do I use LIMIT and OFFSET properly in PDO MySql [duplicate]

This question already has answers here:
How do I calculate the offsets for pagination?
(4 answers)
How to apply bindValue method in LIMIT clause?
(11 answers)
Closed 5 years ago.
I asked a question yesterday titled "How do i use binded variables with MySql LIMIT clause. Sorry, I guess it was not clear enough and got 30 views but no suggestions. So here is what I found after a day of trial and error. First, I discovered that the the LIMIT is the actual physical position of a record in a table not my auto_incremented id_ number. A lot of documentation i read also indicated it was the OFFSET that held the record position, so I wasted a lot of time with weird results. Second, I found documentation with a couple of ways to bind a variable to LIMIT. None of them worked, so I resorted to assigning a $variable to the LIMIT as follows.
("SELECT * FROM $livetable WHERE cust_zip = :cust_zip
HAVING distance < :mydistance ORDER BY client_id ASC LIMIT $start_record_position
, $how_many_records_to_display");
This works for me but any better suggestions, would be appreciated. And I hope this will save someone else a day of figuring this out.

display first n rows with pagination in mysql [duplicate]

This question already has answers here:
Simple PHP Pagination script [closed]
(3 answers)
Closed 6 years ago.
A mysql table contain 100 rows. I am trying to display first 20 rows with 5 rows per page.
I think query should be like this
SELECT * FROM `table` top 20 LIMIT 0, 5
but how can i use this concept. waiting for your help.
You can use LIMIT and OFFSET controls, LIMIT is for max rows you want to show and OFFSET is the starting position:
SELECT * FROM db_name ORDER BY db_table LIMIT 5 OFFSET 0

How do I select the top 5 results that have the maximum entries [duplicate]

This question already has answers here:
Using LIMIT within GROUP BY to get N results per group?
(14 answers)
Closed 8 years ago.
I'm working on a reporting software. I'm using MySQL as its Database backend. In the database there's a column called "location". The location column has a lot of values and I want to display the top 5 locations that have the maximum entries.
For eg. there are 500 entries. Out of which, 100 are from Delhi, 150 from Mumbai, 80 from Jaipur, 60 from Hyderabad and so on. Now, I want to create a table of "Top Locations".
I have tried "select * from table_name limit 5" but this returns the last 5 entries.
Is there any method I'm missing?
I'm on a phone so sorry for the brevity. You can use GROUP BY to accomplish this. Something like:
SELECT location, COUNT(1) AS ct
FROM table_name
GROUP BY location
ORDER BY ct DESC
LIMIT 5

How to return the latest added rows from MySql database. [duplicate]

This question already has answers here:
How can I return 10 of the most recent results in sql?
(4 answers)
Closed 9 years ago.
How can i ask a question to a mysql-database (via PDO) to return the 3 latest added rows? The id column is key and auto incremented. That means that highest id means latest added. Don't take into account the fact that rows can have been deleted and such.
Could i somehow use * and LIMIT 3 and start from the bottom some way - or something?
Should be fairly easy but i am kind of stuck.
Order by ID desc limit 3
gives you the three rows with the highest ID. Those are not necessarily the latest three added rows. But they are the latest three added rows still existing in the table.

Categories