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";
Related
i have a table that has records with its timestamp. now i have a requirement to get the oldest record and the newest record
9/10/2014 6:54
9/10/2014 6:53
9/10/2014 6:51
9/8/2014 8:09
9/8/2014 7:00
9/5/2014 7:38
9/5/2014 3:57
9/5/2014 3:51
9/4/2014 11:09
9/4/2014 8:39
currently this is how i obtain them by sending two database calls which slows downs processing
$new_timestamp = mysql_query("SELECT TIMESTAMP FROM $rectable ORDER BY TIMESTAMP DESC LIMIT 1");
$col_old = mysql_fetch_assoc($new_timestamp);
$old = $col_new['TIMESTAMP'];
$new_timestamp1 = mysql_query("SELECT TIMESTAMP FROM $rectable ORDER BY TIMESTAMP ASC LIMIT 1");
$col_new = mysql_fetch_assoc($new_timestamp1);
$new = $col_new['TIMESTAMP'];
is there any way to optimize this code and fullfiill requirement without sending two database calls, througha special query or a stored proceedure
You can use max and min to get the newest and oldest timestamps
select max(timestamp), min(timestamp) from mytable
Try with UNION
select TIMESTAMP as old FROM $rectracktable ORDER BY TIMESTAMP DESC LIMIT 1
union all
select TIMESTAMP as new FROM $rectable ORDER BY TIMESTAMP ASC LIMIT 1
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
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);