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
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?
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
I have this little script that shows one wisdom each day.
so I have three columns.
Id wisdom timestamp
1 wisdon 1 4/1/2012
2 wisdon 2 4/1/2012
3 wisdon 3 4/2/2012
and I want to fetch array of one wisdom for each day
I looked around your website, but unfortunately I didn't find something similar to what I want.
also I got this code
$sql = mysql_query("SELECT DISTINCT id FROM day_table group by timestamp");
but this also not working.
any ideas?
is it possible to make a counter of 24 hours update wisdom date?
please give me some help.
You can make another table that is called wisdom_of_day
The table would have the following columns, id, wisdom_id, date
Basically each day you can randomly select a wisdom from your wisdom table and insert it into the wisdom day table. You can also add a constraint to your date column so it is distinct. It is important that it is a date column and not a timestamp since you don't care about time.
Then you can retrieve the wisdom of the day by querying based on the date.
It's possible I read your question wrong and you just want to select one wisdom for each day, but you want to show multiple days and you want to get the data from your table.
If so, the reason your query is not working is because you are grouping by a timestamp which includes the date and time. You need to group it by date for it to group like you want.
Here is a query that will group by the day correctly. This will only work if you have a timestamp field and are not storing a unix timstamp on an int column.
select id, wisdom, date(timestamp) date_only from day_table group by date_only order by date_only asc;
Hmm, I noticed that your timestamp values are in some kind of date format, maybe as a string? If so the above query probably won't work.
First compute number of days since 1970
SELECT DATEDIFF(CURDATE(), '1970-01-01')
Then insert this number inside RAND, for example:
SELECT * FROM table ORDER BY RAND(15767) LIMIT 1;
Rand with number as argument is deterministic.
Full query:
SELECT * FROM table ORDER BY RAND((SELECT DATEDIFF(CURDATE(), '1970-01-01'))) LIMIT 1;