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'
Related
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
Is there any way to create a trigger that automatically calculates the sum and updates the rows in database?
For now i have this query which sums my rows and displays a total.
Select
PreAgg.id,
PreAgg.debit,
#PrevBal := #PrevBal + PreAgg.debit As total
From
(Select
YT.id,
YT.debit
From
test.accounts YT
Order By
YT.id) As PreAgg,
(Select
#PrevBal := 0.00) As SqlVars
This gives me:
id debit total
1 1000 1000
2 2000 3000
My question is how can this be converted into a trigger that calculates the sum after every insert and inserts it into the total field? please give me complete detail and query. thanks.
After doing some research i came up with this trigger:
CREATE TRIGGER `update_bal` BEFORE INSERT ON `sp_records` FOR EACH ROW INSERT INTO ledger
SELECT
PreAgg.id,
PreAgg.tot_amnt,
#PrevBal := #PrevBal + PreAgg.tot_amnt as balance
from
( select
YT.id,
YT.tot_amnt
from
sp_records YT
order by
YT.id ) as PreAgg,
( select #PrevBal := 0.00 ) as SqlVars
But it doesn't let me update my table, says "Column count doesn't match value count at row 1" when i insert something in my sp_records table. It works fine though without this trigger.
I have two tables one is sp_records that i want my tot_amnt field from and the other is ledger in which i want to insert the "balance". Both tables have additional fields in addition to the field i have mentioned.
CREATE TABLE `ledger` (
`id` int(11) NOT NULL,
`date` varchar(15) NOT NULL,
`debit` float NOT NULL,
`credit` float NOT NULL,
`balance` float NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
heres my ledger.
I think you can use formula field on your table. Where you want to show the calculation. The reason why I am suggestion this trigger may drop your SQL engine performance.
(I don’t have enough points to add comments that’s the why I am putting as an answer. )
Cheers.
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().
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
I am stuck on an update query that is using a subquery and have not been able to figure it out after reading the manual and trying different ideas. Below is the table & query. Tables Temp_2, Temp_3 & Temp_4 both have 33 rows in them and no null values.
Any ideas on how to resolve this?
CREATE TABLE temp_2 (
date_value date default NULL,
close_adj_value_1 double default NULL);
CREATE TABLE temp_3 (
date_value date default NULL,
first_close_adj_value_1 double default NULL);
CREATE TABLE temp_4 (
date_value date default NULL,
pct_return_1 double default NULL);
INSERT INTO temp_4 (date_value) SELECT date_value FROM temp_2;
UPDATE temp_4
SET pct_return_1 =
(SELECT ((temp_2.close_adj_value_1 / temp_3.first_close_adj_value_1) - 1)
FROM temp_2,temp_3
WHERE temp_2.date_value = temp_3.date_value);
Thanks,
Eric
Since you're using MySQL, you can employ its multi-table UPDATE syntax:
UPDATE temp_4
JOIN temp_2 USING (date_value)
JOIN temp_3 USING (date_value)
SET temp_4.pct_return_1 = (temp_2.close_adj_value_1 /
temp_3.first_close_adj_value_1) - 1;
I assume you want to use the date_value column to correlate the rows in temp_4 to the rows in the other tables.
An alternative solution you could use is to insert all the values into an empty temp_4 table in one go:
INSERT INTO temp_4 (date_value, pct_return_1)
SELECT temp_2.date_value,
(temp_2.close_adj_value_1 / temp_3.first_close_adj_value_1) - 1
FROM temp_2 JOIN temp_3 USING (date_value);
The answer from Bill shown below solved my problem:
INSERT INTO temp_4 (date_value, pct_return_1)
SELECT temp_2.date_value,
(temp_2.close_adj_value_1 / temp_3.first_close_adj_value_1) - 1
FROM temp_2 JOIN temp_3 USING (date_value);