This question already has answers here:
SQL select only rows with max value on a column [duplicate]
(27 answers)
Retrieving the last record in each group - MySQL
(33 answers)
Closed 3 years ago.
I have a problem to show one of duplicate data with last created date. This is is my query :
$sqlpur ="SELECT DISTINCT pn, created_date, id_purchase FROM pur_supp ORDER BY pn, created_date DESC";
This is the result.
This is a data from database.
But, I want to show just one of duplicate data. an example just to show 02N64073 with last created Date.
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
This question already has answers here:
Returning query results in predefined order
(11 answers)
Closed 6 years ago.
I have a IN clause query in which I pass this ID'S (10,19,23,24,28,33,45,46,1,4,7,8,12,16,18,22,29)
My query is,
SELECTpost_jobs.idFROMpost_jobsWHEREidIN(10,19,23,24,28,33,45,46,1,4,7,8,12,16,18,22,29)
It execute correctly and returns result first id likes 1 then 4 then 7 and so on...that is ascending order
My Need is to get first result of id 10 then 19 then 23 as the order of ID that I passed is there any way to get such type of result in output.
Try this
SELECT post_jobs.id FROM post_jobs WHERE id
IN(10,19,23,24,28,33,45,46,1,4,7,8,12,16,18,22,29) ORDER BY
FIELD(id,10,19,23,24,28,33,45,46,1,4,7,8,12,16,18,22,29);
In Codeigniter
$this->db->_protect_identifiers = FALSE;
$this->db->select('id');
$this->db->where_in('id',array(10,19,23,24,28,33,45,46,1,4,7,8,12,16,18,22,29));
$this->db->order_by('FIELD(id, 10,19,23,24,28,33,45,46,1,4,7,8,12,16,18,22,29)');
$this->db->get('post_jobs');
If you are using codeigniter 3 then write or remove the line of protect_identifiers
$this->db->protect_identifiers(False);
Try this one
SELECT post_jobs.id FROM post_jobs WHERE post_jobs.id
IN(10,19,23,24,28,33,45,46,1,4,7,8,12,16,18,22,29) ORDER BY
FIND_IN_SET(post_jobs.id,10,19,23,24,28,33,45,46,1,4,7,8,12,16,18,22,29);
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
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.