I have a question regarding time in MySQL.
How do i get a time stamp like this:
2014-12-07 12:54:42.000000
To correctly insert itself in this table:
The timestamp has to divide to,Hour,Dayname,Weeknumber,Daynumber
I need a fixed value of 1 in every layar of the table
+-------------+----------+--------+-----+-----+-----------x
| fixedval | Hour | Dayname| Weeknumber| Daynumber |
+-------------+----------+--------+-----------+-----------+
| 1 | | | | |
| 1 | | | | |
| 1 | | | | |
| 1 | | | | |
| 1 | | | | |
+-------------+----------+--------+-----------+-----------+
There are functions for that
https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html
Insert into table
(fixedval,hour,dayname,weeknumber,daynumber)
(Select 1, hour(inputdate), dayname(inputdate), weekofyear(inputdate), dayofyear(inputdate))
SELECT HOUR(your_date) as Hour,DAYNAME(your_date) as Dayname,WEEK(your_date) as Weeknumber, DAYOFMONTH(your_date) as datenumber from table.
NOTE: You can use either DAYOFMONTH/DAYOFWEEK for day number.
Related
I am facing a huge problem with MYSQL.
I have a table called tperson with the following content
+--------------+------------+
| tperson_id | first_name |
+--------------+------------+
| 1 | juan |
| 2 | miguel |
| 3 | Carlos |
| 4 | Diego |
+--------------+------------+
on the second table i have this data
+--------------+------------+------------+
| tperson_id | trans_code | date_added |
+--------------+------------+------------+
| 1 | 2000-01 |2020/03/03 |
| 1 | 2000-02 |2020/03/04 |
| 2 | 1999-05 |2019/12/25 |
| 3 | 1999-06 |2019/12/26 |
| 3 | 1999-07 |2019/12/27 |
+--------------+------------+------------+
Now I want to have this result in mysql
+--------------+------------+------------+------------+
| tperson_id | first_name | trans_code | date_added |
+--------------+------------+------------+------------+
| 1 | juan |2000-02 | 2020/03/04 |
| 2 | miguel |1999-05 | 2019/12/25 |
| 3 | Carlos |1999-07 | 2019/12/27 |
| 4 | Diego | null | null |
+--------------+------------+------------+------------+
what is the right MYsql statement to generation the result I want?
pls anyone help, I keep looking for the answer found nowhere. I am not good in any database.
thank you so much
I'm assuming your 2nd table name is tdate, and data on trans_code and date_added that's being selected is the latest value if there are more than one data from the same tperson_id on table tdate
SELECT tp.tperson_id, tp.first_name, MAX(td.trans_code), MAX(td.date_added)
FROM tperson tp
LEFT JOIN tdate td
ON tp.tperson_id = td.tperson_id
GROUP BY tp.tperson_id
i am trying to get the recent 5 orders based on date_of_order and time_of_order fields in the below table and the data type of both these fields is varchar and here order_child_id is some random number which is not stored in order.
How can I write the query for this?
order_details_child table:
+------------------+------------+---------------+---------------+
| order_child_id | vendor_id | date_of_order | time_of_order |
+------------------+------------+---------------+---------------+
| 2000010313044428 | 5060091713 | 2015-03-11 | 16:44:28 |
| 2000010313044920 | 5060091713 | 2015-03-13 | 16:49:20 |
| 2000010313044951 | 5060091713 | 2015-03-11 | 16:49:51 |
| 2002880313043941 | 5000818755 | 2015-03-13 | 16:39:41 |
| 2002880313044029 | 5000818755 | 2015-03-12 | 16:40:29 |
| 3000010313044555 | 5060091713 | 2015-03-12 | 16:45:55 |
| 4000010313044555 | 5000818755 | 2015-03-13 | 16:45:55 |
+------------------+------------+---------------+---------------+
Is this what you are looking for?
select od.*
from order_details od
order by date_of_order desc, time_of_order desc
limit 5;
You should store dates and times in the native format. But if you can't for some reason, your formats are the right way to store them as strings.
I have two tables similar to these (t_stamp would normally be a DATETIME, abbreviated here for clarity):
datapoints
+------+---------+----+---------+
| ndx | value | ID | t_stamp |
+------+---------+----+---------+
| 1 | 503.42 | 1 | 3/1/15 |
| 2 | 17.81 | 2 | 3/1/15 |
| 4 | 498.21 | 1 | 3/2/15 |
| 4 | 19.51 | 2 | 3/2/15 |
+------+---------+----+---------+
parameters
+------+----+---------------+-------+
| ndx | ID | description | unit |
+------+----+---------------+-------+
| 1 | 1 | wetwell level | ft |
| 2 | 2 | effluent flow | MGD |
+------+----+---------------+-------+
I'm looking to combine them so that the descriptions become column headers and list the values in order of time stamp, end result looking something like this:
new table
+---------+---------------+---------------+
| t_stamp | wetwell level | effluent flow |
+---------+---------------+---------------+
| 3/1/15 | 503.42 | 17.81 |
| 3/2/15 | 498.21 | 19.51 |
+---------+---------------+---------------+
Bearing in mind, I have considerably more rows in each table so I'm looking for something dynamic. It could be query or stored procedure based. Thank you for any help!
I'm making the following query with MySQL:
SELECT id, from_unixtime(`timestamp`/1000, '%Y-%m-%d') as day_time, timestamp, end_timestamp, min((`end_timestamp`-`timestamp`)/60000) as min_diff_minutes from table1 WHERE imp<=130;
The table is the following (this table is only a small portion of the original one):
+--------------+------------+---------------+---------------+
| id | day | timestamp | end_timestamp |
+--------------+------------+---------------+---------------+
| 71fce85098b9 | 2014-03-09 | 1394367994962 | 1394368030106 |
| 81fgr85093y6 | 2014-03-09 | 1394367950493 | 1394367993432 |
| 12rtw63423d1 | 2014-02-15 | 1394367302837 | 1394367783463 |
| 77grs82544e7 | 2014-01-21 | 1394367003472 | 1394367903843 |
+--------------+------------+---------------+---------------+
The result of the query is the following:
+--------------+------------+---------------+---------------+------------------+
| id | day | timestamp | end_timestamp | min_diff_minutes |
+--------------+------------+---------------+---------------+------------------+
| 71fce85098b9 | 2014-03-09 | 1394367994962 | 1394368030106 | -23359195.5028 |
+--------------+------------+---------------+---------------+------------------+
How is possible to have
-23359195.5028
as result of the MIN() function, since the value "min_diff_minutes " is just the difference of "end_timestamp" with "timestamp" divided for 60000 ?
PS Anyway, the values "timestamp" and "end_timestamp" are of type BIGINT.
In my access database, we keep track of two sets of dates. One set is for date of membership dues payments, the other set is date of other contributions (a non-membership donation.) There are multiple dates for each person depending on number of payments made for each type.
Example:
+----+---------------+---------------+
| ID | Dues_Date | Cont_Date |
+----+---------------+---------------+
| 1 | 01/01/15 | 09/12/11 |
| | 01/01/14 | |
| | 01/01/13 | |
| 2 | 07/30/14 | 06/20/13 |
| | | 11/12/11 |
+----+---------------+---------------+
First I needed to know the most recent payment for each of the two fields so I ran a query that tells me the MAX (most recent) date for each field.
Example Query:
+----+---------------+---------------+
| ID | Max Dues_Date | Max Cont_Date |
+----+---------------+---------------+
| 1 | 01/01/15 | 09/12/11 |
| 2 | 07/30/14 | 06/20/13 |
| 3 | 02/11/13 | 09/16/14 |
| 4 | 07/30/12 | 06/20/11 |
| 5 | 12/13/13 | 11/12/14 |
+----+---------------+---------------+
Now I need a third field in the same query to compare the results of the first two fields and show which is the MAX of those two.
I have column 2 and 3 in the query; how can I take that and create column 4 in the same query?
Example Query:
+----+---------------+---------------+-----------------+
| ID | Max Dues_Date | Max Cont_Date | Max Date(DD&CD) |
+----+---------------+---------------+-----------------+
| 1 | 01/01/15 | 09/12/11 | 01/01/15 |
| 2 | 07/30/14 | 06/20/13 | 07/30/14 |
| 3 | 02/11/13 | 09/16/14 | 09/16/14 |
| 4 | 07/30/12 | 06/20/11 | 07/30/12 |
| 5 | 12/13/13 | 11/12/14 | 11/12/14 |
+----+---------------+---------------+-----------------+
Try adapting this to your own scenario:
SELECT tblTest.DueDate, tblTest.ContDate, [DueDate]-[ContDate] AS Test, IIf([Test]<0,[ContDate],[DueDate]) AS MaxRes
FROM tblTest;
"Test" finds which is the later date, ContDate or Due Date. The IIf statement selects the later date.
Does this help?