Select * from most recent records - mysql

How to go about selecting all data from the most recent three records in table. This is specifically for a blog that on the home page will show the three most recently dated articles.
ID |title |date |...
---------------------------
1 |exampTitle1|2018-04-25|
2 |exampTitle2|2019-02-10|
3 |exampTitle3|2007-12-21|
4 |exampTitle4|2019-08-10|
The example table shows a subsection of the table, I need to select all data from each record but only from the most recent THREE records, I am aware of MAX() but as far as I am aware that can only get the most recent, not

In SQL, you can order the rows with ORDER BY. Then, you can LIMIT what you are selecting with the LIMIT 3 spesific to SQL of some DBMS products such as MySQL :
SELECT * FROM table ORDER BY date DESC LIMIT 3;

This should work, by sorting by ID in descending order and using LIMIT:
SELECT * FROM <TABLE_NAME> ORDER BY id DESC LIMIT 3

Related

How to fetch records from mysql database in sorted order with pagination along with group by status

I have one table e.g. Employee, which has columns (id, name, is_active).
I want to fetch the records from employee table sorted by name along with pagination.
SELECT * from employee ORDER BY name ASC LIMIT 5, 10;
In above query 5 count to skip first 5 records and take next 10 records.
I want to make a query which will return all records which has value is_active=1, followed by records which has value is_active = 0 with pagination.
e.g: I have 50 records out of them 40 records has value is_active=1, and 10 records which has value is_active=0.
So as I am using pagination and page size is suppose 10 records per page.
So query will return all active records first with order by name and at last page will return 10 inactive records order by name.
Does anyone have any suggestions? Any help would be greatly appreciated.
Thank you!
You just need descendingly sort by is_active column, and no need an OFFSET value depending on the explanation such as
SELECT *
FROM employee
ORDER BY is_active DESC, name
LIMIT 10

Is it possible to order by two fields in a given table,one in ascending and the other in descending order at the same time?

Recently, I have come across a question that has been asked in an interview which states that:
You have mysql database with a table students. Write a query string to
select all the items of the table students and order by two fields one
ascending and the other descending.
Let's have a table "students" for example:
From this example, if we order by Score in descending order then there is no way to order by roll_no in ascending order at the same time.
From the point of view of the question, can there be written any query to obtain the desired result Or is the question ambiguous or wrong or my approach to the understanding of the question is wrong?
In your order by you have asked system to order by First column first so it have ordered then you have asked it to order by second column so it have
-> It have to keep ordering of first column.
-> Order by second column too
So it does ordering within group means if
Table Test
A| B
--------
1 1
1 3
1 2
2 2
2 4
Select * from test order by A desc, B asc
Output
Table Test
A| B
--------
2 2
2 4
1 1
1 2
1 3
So in your case, if you first order by score desc then it will order
First, all students according to there scores descending
And then if two or more students have the same score say 60 then within that group the students will be ordered according to roll number ascending
Hope this clears your doubt.
When you use the order by clause, you can specifiy the direction Asc or desc.
In your example with your sample data, there is no point of order by score, then by roll_no, because there is no duplicate in the score column.
If in your real table there are score wich appeared more than once, you can order by score desc, roll_no.
( asc is the default value)

MYSQL pagination performance

I have the following sample MYSQL table:
id | count_likes
-----------
1 | 30
2 | 95
3 | 60
4 | 60
5 | 22
I want to order the table by column count_likes descending and display 5 rows at a time (this is a sample table so assume thousands of rows).
To achieve this I run the following command:
SELECT * FROM table ORDER BY count_likes DESC, id DESC LIMIT 5
I want to give the option for users to load more rows like loading facebook comments for example (5 rows at a time).
To achieve this I run the following command:
SELECT * FROM table WHERE id NOT IN(values already loaded)
ORDER BY count_likes DESC, id DESC LIMIT 5
This could work well for few pages but I think it's not recommended to have like hundred values in the WHERE NOT IN clause.
If I make the command like this:
SELECT * FROM table WHERE count_likes < 'the last displayed count number'
I could miss some rows which have the same count like the last loaded row.
If I make the command like this:
SELECT * FROM table WHERE count_likes <= 'the last displayed count number'
I could get duplicate values that are already loaded.
If I make the command like this:
SELECT * FROM table ORDER BY count_likes DESC LIMIT offset,5
I may get disorganized or duplicate rows as the count_likes for any row may increase or decrease while other users are manipulating the same page.
What is the best way to load more rows in my case above?
The most accurate one would be the WHERE NOT IN but I don't know if it causes performance issues on large number of members like hundred or even thousand.

specific ordering in mysql

I have a sql statement that brings back ids. Currently I am ordering the id's with the usual "ORDER BY id". What I need to be able to do is have the query order the first 3 rows by specific id's that I set. The order the remaining as it is currently. For example, I want to say the first 3 rows will be id's 7,10,3 in that order, then the rest of the rows will be ordered by the id as usual.
right now i just have a basic sql statement...
SELECT * from cards ORDER BY card_id
SELECT *
FROM cards
ORDER BY
CASE card_id WHEN 7 THEN 1 WHEN 10 THEN 2 WHEN 3 THEN 3 ELSE 4 END,
card_id
A bit shorter than Quassnoi's query, with FIELD :
-- ...
ORDER BY FIELD(card_id, 3, 10, 7) DESC
You have to invert the order because of the DESC, I didn't find a way to do it more naturally.

Fetching multiple records from MAX mysql keyword?

I have only one table with the name of offers and it has multiple offers in it like each time we pull in an offer, we create a new row for example: for travelling to Timbuktu, there can be 10 or more rows each containing an offer, each time a offers comes in, it is being saved with PHP unix timestamp in the column name 'created_on', so to figure out which offer is latest, I am currently using following query:
SELECT * FROM offers WHERE city= 'Timbuktu' AND created_on=(SELECT max(created_on)from offers WHERE city = 'Timbuktu')
This serves the purpose if I have to fetch only one latest row, if say I want to fetch last 4 or 8 rows with the greatest timestamp, how I can do that in most efficient way?
SELECT *
FROM offers
WHERE city= 'Timbuktu'
order by created_on desc
limit 0, 8
and for 1 row you can use same request just replace 8 with 1
SELECT * FROM offers WHERE city='Timbuktu' ORDER BY created_on DESC LIMIT 4;