Compare fileds with REGEXP in mysql 5.0 - mysql

I would like to so a nested mysql request that is counting actions for each day like this:
SELECT `timestamp` AS `date`,
(SELECT COUNT('id')
FROM `actions` WHERE `timestamp` = `date`) AS `action_num`
FROM `actions`;
That would work great if it wasn't so that I need to calculate each day and all I got is each timestamp. If it was only a date it would work. So I thought if I can take out the date from the string and compare it this might work:
SELECT `timestamp` AS `date`,
(SELECT COUNT('id') FROM `actions`
WHERE `timestamp` REGEXP '[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2}' = `date`
REGEXP '[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2}') AS `action_num`
FROM `actions`;
But it did not work.
Any ideas on how to compare the dates from two timestamp fields directly in MySQL?
Thanks

Very easy, the inner query should be:
SELECT count(id) FROM actions
WHERE DATE(`timestamp`) = DATE(`date`) as action_num
See: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date

You can use DATEDIFF() function to compare the dates in MySQL,
and TIMESTAMPDIFF() for comparing timestamps , DATE() to get date information from timestamp.
More date and time functions and examples are available in MySQL's reference manual pages.

Related

mysql use wildcard in request involving timestamp

im trying to select all the rows having a particular year in mysql.
the datetime is in a format such as 2016-12-02 10:00:00 so i am trying to use wildcard to do it. However i tried
SELECT * FROM all-data.prices where YEAR(timestamp)= "2005";
and SELECT * FROM all-data.prices where timestamp= "2005%" ;
and it didnt work for me.
Any suggestion on which is the correct syntax for such select statement?
It is not the best idea to use a FUNCTION on a field in the WHERE Clause. Then MySQL must calculate YEAR(timestamp) from every ROW before it can compare. So it is a FULL TABLE SCAN and cant use a INDEX.
A better was is to compare the timestamp with a Range
SELECT * from your_table WHERE `timestamp` >= '2015-01-01 00:00:00' AND `timestamp` < '2016-01-01 00:00:00';
Then MySQL can use a INDEX (if there is one)
To use % you also must use the LIKE Keyword
SELECT '2015-01-01 23:50:00' LIKE '2015%';
in your sample
SELECT * from your_table WHERE `timestamp` LIKE '2015%';

splitting date and inserting individual field in separate column in mysql

i have created a date table directly through phpmyadmin and i want to split the date(yyyy:mm:dd) into individual day, month and year and insert into separate field. Can anybody help me in the query used directly in phpmyadmin to perform above work?
Try:
UPDATE `table` SET `year` = YEAR(`date`), `month` = DATE_FORMAT(`date`, '%m'), `day` = DATE_FORMAT(`date`, '%d')
I've used DATE_FORMAT() rather than MONTH() and DAY() as the latter two remove leading zeros.

Returning the closest to a date in a table using PDO Mysql/MSSQL

I have 2 PDO database connections. I am doing a search within a MS SQL table for a row that closest matches a date (mysql datetime) row.
I have mysql.table1.date passed to mssql.table and I am looking for the closest date accordingt to the mssql.table.date. It is also defined as a datetime field. I only need 1 row returned, the closest to the time, so in essence:
SELECT * FROM table ORDER BY CLOSEST(mysqldate = mssql.table.date) LIMIT 1;
I know the syntax above is incorrect but that basically outputs what I need, but I really do not know how to do this with mssql.
Any help?
Basically u can find the difference of the mysql date with all the dates in mssql.Table.Date column .Then u need to select the least difference value from the above query .Hopefully the below query might help u
;with CTE as
(
Select mssql.table.date,row_number()
over (order by abs(datediff(day,mysqlDate,mssql.table.date))) rowNumber
from mssql.Table)
select mssql.table.date from CTE where rowNumber=1
A simple solution which worked for me was to do the following:
SELECT * FROM `table` WHERE `date` < `startDate` ORDER BY `date` LIMIT 1;
This returns 1 row matching the closest time to the time I am passing :)

Querying Where date_created without time is same as last_updated without time

In MySQL, I need to write a query (if possible) that finds all rows of a table where the date_created is the same as last_updated. The rub is that I need to ignore the time. Basically, I'm looking for user rows that were created and activated the same day (we don't store an activation date). So presumably the dates would be the same but the times may be different.
You could use the DATE() function, which returns only the date portion of a datetime value. This allows you to compare just the date portion of the values:
SELECT * FROM table_name
WHERE DATE(date_created) = DATE(last_updated)
The timezone may be relevant here. So you may want to cast the datetime values to the user's timezone prior to using the DATE() function, using CONVERT_TZ().
Try this:
SELECT *
FROM table_name
WHERE DATE_FORMAT(date_created, '%Y-%m-%d') = DATE_FORMAT(last_updated, '%Y-%m-%d')
not pretty but works:
SELECT *
FROM table_name
WHERE day(date_created) = day(last_updated) and
month(date_created) = month(last_updated) and
year(date_created) = year(last_updated)

MySql, combining date and time column into a time stamp

I am guessing this is relatively simple to do, but I am unsure of the syntax. I have date and time columns that I want to combine to a timestamp column. how would I query this using a select?
Or you could use the built-in TIMESTAMP(date,time) function.
So then you would do something like this say from an Orders table...
SELECT OrderNumber, TIMESTAMP(date,time) as OrderTS, SalesPersonID
FROM Orders
Mysql does not seem to have a constructor for datetime such as datetime('2017-10-26', '09:28:00'). So you will have to treat the component part as string and use string concatenation function (Note mysql does not have the || operator for string concatenation). If you want the datetime type, you will have to cast it.
concat(datefield,' ',timefield) as date
select cast(concat('2017-10-26', ' ', '09:28:00') as datetime) as dt;
If it possible to use built-in function, just use it.
Any way here is an example to find records between given timestamps.
SELECT `id` FROM `ar_time` WHERE TIMESTAMP(`cdate`,`ctime`) BETWEEN fromTimeStamp AND nowTimeStamp;
For 24hr time
TIMESTAMP(Date, STR_TO_DATE(Time, '%h:%i %p'))
SELECT * FROM tablename WHERE TIMESTAMP(datecol, timecol) > '2015-01-01 12:00:00';
O.P. did say SELECT but in case anyone wants to add a timestamp column:
ALTER TABLE `t` ADD COLUMN `stamp` TIMESTAMP;
UPDATE `t` SET `stamp` = STR_TO_DATE(CONCAT(`Date`, ' ', `Time`), '%m/%d/%Y %H:%i:%s');
Adjust format strings to taste.
concat('2021-12-31', ' ', '07:00:00')
it worked in an INSERT procedure.