Mysql minimum function - mysql

Is there a predefined MySQL function that returns minimum of its arguments' values (MINIMUM(1,16) -> 1)?
To be more specific, I have a time-on-site column in one of my mysql tables.
Every visitor polls my server every 30 sec making an update:
UPDATE `mytable` SET `lastUpdate` = NOW() WHERE `id` = ?;
but I'd like to update also timeOnSite column like this:
UPDATE `mytable` SET `timeOnSite` = (
`timeOnSite` + MINIMUM(
TIMESTAMPDIFF(SECOND, lastUpdate, NOW()), 30
)
),
`lastUpdate` = NOW() WHERE `id` = ?;
But the problem is that there are no such MINIMUM function, and I failed to find it in MySQL manuals.

That's because its called LEAST() to avoid confusion with the aggregate function MIN().

Related

Mysql Add value to Float datatype

I am trying to update balance of user based on no of shares(count) they have in share table multiflying with 0.10 For this I have written following statement.I wonder! it is working perfect in sql but whenever i am trying it to mysql it works first time whenever user balance is zero but after that it doesn't work or add value.In mysql i am using float datatype in balance table.Below is my statement.
update `balance`
set `Balance`='Balance'+(select COUNT(*) from `share`
where `user` = `balance`.`User` and `status`='Active')*'.10'
I have found my answer. It is actually the comma between balance is doing the problem.
Below is my working code:
UPDATE `balance`
SET `Balance` = Balance + (
SELECT COUNT(*) FROM `share`
WHERE `user` = `balance`.`User` AND `status`='Active'
) * '.10'

mySQL str_to_date() function returns error

I keep receiving an error message when trying to convert a column, CreatedDate, of string date values in my Estimates table into the mySQL date format using str_to_date(). My column of data contains dates in m/d/yy format (for example: 1/26/16 or 3/3/16).
I ran this query:
UPDATE Estimates
SET CreatedDate = str_to_date( CreatedDate, '%c/%e/%y' )
mySQL is returning this error message:
Error
SQL query:
UPDATE Estimates
SET CreatedDate = str_to_date( CreatedDate, '%c/%e/%y' )
MySQL said: #1411 - Incorrect datetime value: '' for function str_to_date
What is wrong with my query?
Disable NO_ZERO_DATE SQL mode:
set #old_sql_mode = ##sql_mode;
set sql_mode = '';
Run your statement:
UPDATE Estimates
SET CreatedDate = NULLIF(str_to_date(CreatedDate, '%c/%e/%y'), FROM_DAYS(0))
Then enable original SQL modes:
set sql_mode = #old_sql_mode;
Disabling NO_ZERO_DATE mode will make STR_TO_DATE return zero date 0000-00-00 for invalid date strings, the same value is returned by FROM_DAYS(0). So NULLIF will convert zero dates to NULL.
This answer was helpful.
The usual strategy for cleaning up data like this is as follows:
ALTER TABLE Estimates CHANGE COLUMN CreatedDate CreatedDateString VARCHAR(255);
ALTER TABLE Estimates ADD COLUMN CreatedDate DATE
UPDATE Estimates SET CreatedDate=STR_TO_DATE(CreatedDateString, '%c/%e/%y'))
WHERE CreatedDateString IS NOT NULL AND CreatedDateString != ''
Then when you're confident everything got converted correctly:
ALTER TABLE Estimates DROP COLUMN CreatedDateString
The advantage to proper DATE fields is they're in a consistent format and when you add an INDEX on them data retrieval is very fast, even on ranges, like:
SELECT * FROM Estimates WHERE CreatedDate BETWEEN '2016-01-01' AND '2016-06-30'
It's hitting blank values in your column.
SET CreatedDate = str_to_date( '', '%c/%e/%y' )
I think this outputs 0000-00-00 and that works as an invalid date if you are setting a date field to that.
SET CreatedDate = STR_TO_DATE( IFNULL(case when CreatedDate = '' then null else createddate end,'1901-1-1'), '%c/%e/%y' )
That will leave 1901-01-01 values for nulls and blank
Added to tadman:
SET CreatedDate = STR_TO_DATE(case when CreatedDate = '' then null else createddate end, '%c/%e/%y' )
Nulls instead of 1901-01-01 if you prefer.

How to update timestamp values in column by adding a specific time (seconds) to the existing timestamp using mysql?

I am using mysql and pma. I have a table mytable and a column time, storing ~17K individual values, i.e. timestamps (integers).
I need to update each by adding 962758 to each timestamp. What does the SQL command for that look like?
SELECT (*) FROM `mytable` t1
UPDATE `mytable` SET time = + 962758
PROFIT? :)
Would you need a SELECT statement for that or does it work with UPDATE only?
I cant use php for that in this case.
Considering that it's TIMESTAMP datatype, you can say
UPDATE `mytable` SET time = time + INTERVAL 962758 seconds;
Per your comment, since it's of INT type; you can just do the addition likewise you are already doing.
UPDATE `mytable` SET `time` = `time` + 962758;
If the data is stored as a datetime value, then you simply can use:
select timestampadd(second, 962758, time)
If the value is a unix timestamp, then it is already in seconds, and you can just add 962758.
According to your comment the field is int
UPDATE `mytable`
SET time = time + 962758;
And no need select

Convert Mysql Date (NOW()) to bigint

i have a mysql trigger that updates a table when there's an insert in another table. My question is this, how can i convert the current date to bigint value, because it's the bigint value that needs to be updated. This is the update statement
UPDATE clocks SET last_clock_upload = NOW() WHERE clock_id = NEW.clock_id
How can i change the NOW() to bigint?
Try this:
SELECT UNIX_TIMESTAMP(NOW( ) )

Update a field based off of the current value (in mysql)

I have a table similar to
CREATE TABLE `mytable` (
`ID` int(11) unsigned NOT NULL AUTO_INCREMENT,
`Time` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
KEY `time` (`Time`),
) ENGINE=MyISAM AUTO_INCREMENT=2373485 DEFAULT CHARSET=latin1
I had a weird issue with daylight savings time, and now I need to update rows with IDs 2370144 through 2373391 so that the Time values are six hours less than their current values.
I can select the affected rows with
SELECT * FROM mytable WHERE ID >= 2370144 AND ID <= 2373391
How do I update these entries so that the new timestamp is six hours less than the old value?
I think this will work
UPDATE mytable SET Time = date_sub(Time, INTERVAL 6 HOUR) WHERE id BETWEEN 2370144 AND 2373391;
UPDATE mytable
SET `Time` = (`Time` - INTERVAL 6 HOUR)
WHERE ID >= 2370144
AND ID <= 2373391
Expanding on this a little bit, when feasible I would typically run a SQL query to generate a .sql file that contains one update statement per row, then execute that sql file to update the rows. Since you are only updating about 3,000 rows this should be feasible for you.
This dump and load approach has a couple of benefits:
You can save the SQL script as an
audit record of what you changed.
You can include both the ID and the Time
value in the SQL script. That way if
you accidentally run the script more
than once you don't end up changing
the value to something incorrect. For
example, if you ran my original
update twice the values would end up
6 hours too low, but if you use the
dump-and-load approach and run the
script twice, the second time it
won't change the records because the
where clause will no longer match.
Here's an example of the dump-and-load approach:
select concat('update mytable set `Time` = ''',
`Time` - interval 6 hour,
''' where id = ',
id,
' and `Time` = ''',
`Time`,
''';') as sql_stmt
into outfile '/tmp/mytable.update.dstfix.20110315.sql'
from mytable
WHERE ID >= 2370144
AND ID <= 2373391;
\. /tmp/mytable.update.dstfix.20110315.sql