This is kind of straight forward.
I want to ORDER BY date DESC Limit 4 and then I want to ORDER BY date ASC on that result, so just 4-games from the middle of the big table with ASC date order, any ideas?
Just ORDER BY date ASC Limit 4 Does not work!
What I have:
What I get:
What I want:
you can use subquery :
SELECT a.* FROM (SELECT * FROM yourtable ORDER BY date DESC Limit 4) a ORDER BY a.Date
if you want to get the last four rows from table
select * from table order by date desc limit 0,4
if you want to get the first four rows from table
select * from table order by date asc limit 0,4
Related
I want to limit the array to 3 which is been fetch used Group by.
The query is as below:
$sql_time = "SELECT date FROM tb_player_activity WHERE user_id='$data[1]' GROUP BY date DESC";
I want to display only 3 records of the array which is been fetched
Use mysql limit
$sql_time = "SELECT date FROM tb_player_activity WHERE user_id='$data[1]' GROUP BY date DESC limit 3";
Use LIMIT
$sql_time = "SELECT date FROM tb_player_activity
WHERE user_id='$data[1]'
GROUP BY date DESC
LIMIT 3";
MySQL LIMIT Clause. In MySQL the LIMIT clause is used with the SELECT
statement to restrict the number of rows in the result set. The Limit
Clause accepts one or two arguments which are offset and count.The
value of both the parameters can be zero or positive integers.
https://www.geeksforgeeks.org/php-mysql-limit-clause/
SELECT date FROM tb_player_activity WHERE user_id='$data[1]' GROUP BY date DESC limit 0,3
Note: Passing the paramters like this would lead to SQL Injection
try limit for it:
$sql_time = "SELECT date FROM tb_player_activity WHERE user_id='$data[1]' ORDER BY date DESC LIMIT 3";
I have to select top 5 records and display them in ascending order by entry date.
Below is my query:
select id,name,entry_date from users order by entry_date desc limit 0,5
it gives me latest entered 5 users. but I want them in increasing order by entry date.
How to get them in ascending order with same query ?
This will work as desired
SELECT * FROM
(
select id,name,entry_date from users order by entry_date desc limit 0,5
) as a ORDER BY a.entry_date asc
I have a mysql DB with tables, of which in the one table I have a date type field, I want the most recently passed date - so I want it to order by dates descending, but only take records from before today, and then take only the top most one using the LIMIT function, and also there is the addition of the WHERE clause being that the offer must be for the selected city.
$result = mysql_query("
SELECT * FROM offers
WHERE city = ".$_SESSION["city"]."
ORDER BY exp_date DESC
LIMIT 0, 1");
ADD another condition to where clause
$result = mysql_query("
SELECT * FROM offers
WHERE city = ".$_SESSION["city"]." and Date < CURRENT_DATE()
ORDER BY exp_date DESC
LIMIT 1");
SELECT * FROM deals WHERE city = 2 AND exp_date < CURDATE()
ORDER BY exp_date DESC LIMIT 0, 1
Add the following condition to Where:
... and exp_date < CURDATE()
See http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html.
I am trying to select rows with the 20 highest 'TimeStamp' value, and from those 20 rows the 1 row with the lowest ID value:
$result_last = mysql_query("SELECT * FROM (SELECT * FROM Events ORDER BY TimeStamp DESC LIMIT 20) ORDER BY ID ASC LIMIT 1");
The above query doesn't work, but it makes sense to me. Is there something wrong with this query?
The error is #1248 - Every derived table must have its own alias and this is indeed true. This should work:
$result_last = mysql_query("SELECT * FROM (SELECT * FROM Events ORDER BY TimeStamp DESC LIMIT 20) AS T ORDER BY ID ASC LIMIT 1");
Try something like this:
$result_last = mysql_query("SELECT * FROM Events WHERE TimeStamp IN(SELECT TimeStamp FROM Events ORDER BY TimeStamp DESC LIMIT 10") ORDER BY TimeStamp ASC LIMIT 1);
i want to minimize my query
"SELECT * FROM tb_videos GROUP BY DATE(added) ORDER BY DATE(added) desc"
after get result
foreach($result as $rst)
{
$dt=date('Y-m-d',strtotime($rst->added));
SELECT * FROM tb_videos WHERE DATE(added)='$dt' ORDER BY added desc
}
Can i do with single query?
Your queries make no sense. First you are selecting the DISTINCT dates (ignore time) from the data. Then for each date, you.. select all data for that date?
Why don't you just fire one query
SELECT *, DATE(added) as DateNoTime
FROM tb_videos
ORDER BY added desc
If you only want 5 dates, and the table is large, there are two possibilities.
1, There are never gaps in dates, you can use
SELECT *, DATE(added) as DateNoTime
FROM tb_videos
WHERE added >= ADDDATE(CURDATE(), interval -4 days)
ORDER BY added desc
2, If there may be gaps, e.g. nothing for yesterday so it has to show last 5 days that have records
SELECT *, DATE(added) as DateNoTime
FROM (
select min(DateNoTime) as MinDate
from
(
select DATE(added) as DateNoTime
FROM tb_videos
order by DateNoTime desc
limit 5
) x) y, tb_videos
WHERE added >= y.MinDate
ORDER BY added desc
This gives you all the data. In PHP, keep track of DateNoTime. Whenever that changes, you are in a different date, which would have previously caused you to fire another query. The code should otherwise only change minimally.
Unchecked PHP code
$result = mysql_query('
SELECT *, DATE(added) as DateNoTime
FROM (
select min(DateNoTime) as MinDate
from
(
select DATE(added) as DateNoTime
FROM tb_videos
order by DateNoTime desc
limit 5
) x) y, tb_videos
WHERE added >= y.MinDate
ORDER BY added desc
');
$prevdate = NULL;
foreach($result as $rst)
{
if($prevdate!=$rst=>DateNoTime) {
// do something, like printing out a header for each new date
$prevdate=$rst=>DateNoTime;
}
// do something with the record
}
You want to minimise the records found? use a where clause to choose your specific records
"SELECT * FROM tb_videos WHERE videogroup = action GROUP BY DATE(added) ORDER BY DATE(added) desc"
For example
SELECT * FROM tb_videos WHERE DATE(added) = curdate() ORDER BY added desc