So I have this php code:
"SELECT * FROM thread_db ORDER BY id DESC LIMIT 3";
This echos me later the last 3 rows of my mysql database. I somehow need it to get sorted by time so I get the 3 latest entries by time (closest to current date).
Try this
"SELECT * FROM thread_db ORDER BY your_time_field DESC LIMIT 3";
Replace your_time_field with the exact name of your time column in the query
Related
I have a history table with 3700000+ entries. I am using server-side processing in the datatable to initially display 25 records. It is taking lots of time to initially load the datatable even though the query fetches only 25 records.
I am using MySQL database. Now I want to limit the total number of entries from which the data should be processed. I want only past 15 days entries to be considered.
Is there a way by which I can load the table quickly?
There are a number of ways you can limit how much data you get back from a SQL Query.
Using WHERE will allow you to only select entries after a certain date.
$sql = "SELECT * FROM history WHERE date > ".strtotime("-15 days");
The above query assumes that in your history database you have a date that is a unix based timestamp. Another way that you could limit the amount of data that is returned is by using the LIMIT function.
$sql = "SELECT * FROM history ORDER BY id DESC LIMIT 15";
This query assumes that you only have one entry per day, and will limit it to only 15 entries to be displayed.
Along with this, if your history table has a lot of data that doesn't need to be used, then you can only select the columns that you need, doing something like this:
$sql = "SELECT id, time, name FROM history";
SELECT * FROM $table_name WHERE created_date>= DATE(NOW() - INTERVAL 15 DAY)
I have a MySQL database which stores LogID, unix timestamp, temperature, humidity and light level. I am trying to get a query that extracts the last record before a set date & time. My data is as follows:
|69511|2017-04-24 19:53:23|19.8|52.7|1.76
|69512|2017-04-24 20:07:57|20|53.8|1.86
|69513|2017-04-24 20:12:00|20.1|54.9|1.07
|69514|2017-04-24 20:29:58|20.2|53.8|1.95
So if the required date was 2017-04-24 20:10:00 the query would return the record:
|69512|2017-04-24 20:07:57|20|53.8|1.86
as the first record preceding 2017-04-24 20:10:00.
Can anyone help? Thanks.
Use a WHERE clause to limit your search to records before the date in question, then sort in descending order by the timestamp (sorting by ID will probably work as well) and take the first record with LIMIT 1.
SELECT * FROM your_table
WHERE ts_column < '2017-04-24 20:10:00'
ORDER BY ts_column DESC LIMIT 1
I improvised the name of your table & timestamp column, but this should give you the general idea.
Perhaps try:
SELECT * FROM {tablename} WHERE {timestamp column} < '2017-04-24 20:10:00' LIMIT 1
EDIT: add 'order by unix_timestamp desc'
Filter the records in where clause using the timestamp. Sort it by descending order and get the top 1 record using limit.
select * from table_name
where unix_timestamp < '2017-04-24 20:10:00'
order by unix_timestamp desc
limit 1
I have a column in a MySQL database table that has times (just time of the day ex. "15:45:00") however when I am sorting by time, it doesn't seem to order the rows correctly in order of time. Here is a screenshot from the query:
SELECT * FROM stop_times ORDER BY day ASC, departure_time ASC
Do I need to cast the column in a certain form or convert the time in a way for this to work?
I have to get the row with the latest date less than a given date selected from a select box.
I have figured out how to get the row with the latest date like
select * from test_table where created in (select max(created) from test_table)
I know I have to use a created between but I have not been able to solve it. The date which is the upper limit is selected from a select box. SO, I dont know it before hand.
Please help me
for example, let the date selected from textbox is Nov 11 2013. I have to get a row with a created date which is the max date in the table less than Nov 11 2013.
Thanks in advance
This query will return the rows that have the latest datetime less than #givendate:
SELECT *
FROM test_table
WHERE created = (SELECT max(created)
FROM test_table
WHERE created < #givendate)
please notice that there could be more than one row with the same latest datetime. Or you could simply use this:
SELECT *
FROM test_table
WHERE created < #givendate
ORDER BY created DESC
LIMIT 1
try this
select *
from test_table order by created desc
limit 1
To get the second last entry use the offset of the limit function
select *
from test_table
order by created desc
limit 1,1
I have a following query
$query = "SELECT * FROM phpvms_schedules ORDER BY deptime + 0 ASC";
In the table, I call $list variable to get database results from the above variable. I then, have a foreach statement
foreach($list as $flight)
One of the columns echoes departure time value of a flight in a HH:MM format.
<td>'.$flight->deptime.'</td>
Essentially, I want to order the times in ascending order, going up, but I'm probably missing a step, because it doesn't do it. It does it like this:
17:30
17:55
17:15
17:45
17:25
I performed a small check on the $query changing ASC to DESC, but the same thing happens, obviously with descending times.
Change deptime from VARCHAR to TIME (proper way)
Query like ORDER BY deptime or ORDER BY deptime ASC