Working on mysql.5.7
Here is my bugs table
MySQL [jira_statistics]> describe bugs;
+---------------------------------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------------------------------+--------------+------+-----+---------+-------+
| issue_key | varchar(45) | NO | PRI | NULL | |
| release_name | varchar(45) | YES | MUL | NULL | |
| issue_summary | varchar(200) | YES | | NULL | |
| story_points | int(11) | NO | | 0 | |
| qa_reopened | float | NO | | 0 | |
| done_reopened | float | NO | | 0 | |
This table is updated by periodic calls to LOAD DATA LOCAL INFILE bugs <file.csv>
Whenever this update takes place (which may either update existing lines and/or insert new ones) I want another table that has some yielded statistics to be updated via the following trigger
create trigger update_bugs_stats after insert on `jira_statistics`.`bugs` for each row
begin
delimiter ;
-- STORY POINTS -------------------------
SELECT AVG(story_points) INTO #avg_bugs_storypoints FROM `jira_statistics`.`bugs` WHERE release_name = new.release_name;
SELECT MAX(story_points) INTO #max_bugs_storypoints FROM `jira_statistics`.`bugs` WHERE release_name = new.release_name;
SELECT MIN(story_points) INTO #min_bugs_storypoints FROM `jira_statistics`.`bugs` WHERE release_name = new.release_name;
INSERT INTO storypoints_stats (release_name, avg_bugs_storypoints, max_bugs_storypoints, min_bugs_storypoints)
VALUES (relName, #avg_bugs_storypoints, #max_bugs_storypoints, #min_bugs_storypoints)
ON DUPLICATE KEY UPDATE
relName=new.release_name,
avg_bugs_storypoints=#avg_bugs_storypoints,
max_bugs_storypoints=#max_bugs_storypoints,
min_bugs_storypoints=#min_bugs_storypoints;
However this gives me the following error whenever trying to create the trigger:
Unknown column new.release_name in where clause.
Why isn't the new keyword bein recognized?
Because new is reserved as a system word
Ref: https://dev.mysql.com/doc/refman/8.0/en/keywords.html
Please modify
new.release_name ==> `new`.`release_name`
etc..
Τhe error was more stupid than I thought;
I was working directly on sql query editor and not on the triggers tab of mysql workbench so it did not parse correctly the new keyword`.
Related
I've created a new table like this:
+------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| first | varchar(100) | NO | PRI | NULL | |
| last | varchar(400) | NO | PRI | NULL | |
| source | varchar(100) | NO | | NULL | |
| count | int | YES | | 1 | |
+------------+--------------+------+-----+---------+-------+
And I try to insert multiple records to this table using this:
insert into my_table(first,last,source,count) values ('a','b','c',50),('a','b','c',20),('d','e','f',30) on duplicate key update count = count + 1;
After insert, this is the content of the table:
+------------+-----------+--------+-------+
| first | last | source | count |
+------------+-----------+--------+-------+
| a | b | c | 2 |
| d | e | f | 1 |
+------------+-----------+--------+-------+
However, I'd like the count to be updated by the numbers provided in the values of the new records (i.e., 50, 20, and 30 in the provided example). So, the table should look like this:
+------------+-----------+--------+-------+
| first | last | source | count |
+------------+-----------+--------+-------+
| a | b | c | 70 |
| d | e | f | 30 |
+------------+-----------+--------+-------+
Is it possible to achieve this using "on duplicate key update" in MySQL? Or is there any other efficient way to achieve this? The table will be very large (with millions of rows).
VALUES() is the method to use, as GMB mentioned, if you are on a mysql version older than 8.0.19. However, it was deprecated as of 8.0.20, if you are using mysql 8.0.19 or newer its recommended to give an alias to the rows being inserted, and then refer to the values of the inserts by the alias like this:
insert into my_table (first, last, source, count)
values ('a','b','c',50), ('a','b','c',20), ('d','e','f',30) as newRow
on duplicate key update count = count + newRow.count;
More information can be found here: https://dev.mysql.com/doc/refman/8.0/en/insert-on-duplicate.html
Consider the VALUES() syntax, that you can use in the on duplicate key clause to refer to the column value that would otherwise have been inserted:
insert into my_table(first, last, source, count)
values ('a','b','c',50), ('a','b','c',20), ('d','e','f',30)
on duplicate key update count = count + VALUES(count);
Note: first, last and source are MySQL keywords. I would not recommend using them as column names.
I'm struggling with a simple update command to a row in a table called lychee_settings, table description is:-
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| key | varchar(50) | NO | | | |
| value | varchar(200) | YES | | | |
+-------+--------------+------+-----+---------+-------+
row I want to change is (0 to 1):-
| skipDuplicates | 0 |
I am running
UPDATE lychee_settings SET value = '1' WHERE key = 'skipDuplicates';
which returns an
You have an error in your SQL syntax.
I can't see what I am doing wrong, must be something very simple, any help much appreciated!
key is a reserved word in MySQL so if you have to use it as a column name (not recommended) you have to wrap that column name is backticks.
UPDATE lychee_settings SET value = '1' WHERE `key` = 'skipDuplicates';
I have a table of this structure:
mysql> desc securities;
+-----------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+-------------+------+-----+---------+-------+
| sym | varchar(19) | NO | PRI | | |
| bqn | int(11) | YES | | NULL | |
| sqn | int(11) | YES | | NULL | |
| tqn | int(11) | YES | | NULL | |
+-----------------+-------------+------+-----+---------+-------+
4 rows in set (0.01 sec)
I am trying to do a select and an update within the same query, so the reason I have chosen
insert into securities (sym, bqn, sqn , tqn) values('ANK', 50,0,1577798)
on duplicate key update bqn=bqn+50 , sqn=sqn+0 , tqn=tqn+1577798;
When I ran the above I observed it is in fact changing the values for all the other rows also.
Is this behaviour expected? I am using MySQL Database.
Your fiddle is missing the key, and the INSERT statement in the right panel (where it does not belong in the first place) is using different column names … *sigh*
Define the symbol column as PRIMARY KEY – and use the VALUES() syntax to get the values to add in the ON UPDATE part, so that you don’t have to repeat them every single time:
insert into securities
(symbol, buyerquan, sellerquan , totaltradedquan)
values('BANKBARODA', 73, 0, 4290270)
on duplicate key update
buyerquan=buyerquan+VALUES(buyerquan),
sellerquan=sellerquan+VALUES(sellerquan),
totaltradedquan=totaltradedquan+VALUES(totaltradedquan);
Works perfectly fine, result values are as to be expect from the input: http://sqlfiddle.com/#!2/21638f/1
I'm sorry for such a newbie question, but I have two tables:
vtiger_assets
+-----------+---------+------+-----+---------+----------------+
| assetid | account | Stat | Key | Default | assetname |
+-----------+---------+------+-----+---------+----------------+
| 224 | int(11) | NO | PRI | NULL | |
| 225 | int(11) | NO | | NULL | |
| 226 | date | NO | | NULL | |
| 227 | date | NO | | NULL | |
| 228 | int(11) | NO | | NULL | |
| 229 | int(11) | NO | MUL | NULL | |
| 230 | int(11) | NO | MUL | NULL | |
+-----------+---------+------+-----+---------+----------------+
And vtiger_assetscf
+--------------+---------+
| assetid | cf_658 |
+--------------+---------+
| 224 | Value 1 |
| 225 | Value 2 |
| 226 | Value 3 |
| 227 | Value 2 |
| 228 | Value 3 |
| 229 | Value 1 |
| 230 | Value 3 |
+--------------+---------+
After one day of trying and errors using Trigger, INSERT and UPDATE I give up and decided to ask the experts...
A new row is added in both tables at the same time (with a new assetid automatically added)
I need to automatically import and update (populate) the values from cf_658 column at vtiger_assetcf table to the assetname column at vtiger_assets table.
I have tried:
create trigger 'ativos' after insert on vtiger_assetscf
for each row
begin
insert into vtiger_assets (assetid, assetname) values (new.assetid, new.cf_658);
end#
I have tried a combination of INSERT and UPDATE. No luck...
Can someone help me?
You have used single quotes around the name. That should cause a syntax error. Try this:
delimiter #
create trigger ativos after insert on vtiger_assetscf
for each row
begin
insert into vtiger_assets(assetid, assetname)
values (new.assetid, new.cf_658);
end; #
I hope you have already solved your problem. If not, I may have some questions to clarify:
1.- What do you mean with
A new row is added in both tables at the same time[...]
Is it that you use one call to the DB in which you insert both rows? Because I guess you are doing two queries on one call. One that inserts into vtiger_assets and then one that inserts into vtiger_assetscf
If it is the case, maybe you are triggering the ativostrigger before you have the row from wich to copy the data.
2.- With
with a new assetid automatically added
I supose you have your auto_incrementproperty enabled for the assetid column, but this makes me wonder, why do you have separate tables if one just copy data recently sent?
Maybe you could send one query only to vtiger_assets and have a trigger in that table (not into vtiger_assetscf) that creates a new row into vtiger_assetscf (but now I think maybe your tables are not completely shown here....)
3.- Finally: Is your assetid a key column? (if you're using auto_increment, I bet it is). If it's the case, I think the error you get is that you try to insert a row in a table with a duplicate value (the assetid).
Maybe try this instead:
create trigger `ativos` after insert on `vtiger_assetscf`
for each row
begin
update `vtiger_assets` SET `assetname` = new.`cf_658` WHERE `assetid` = NEW.`assetid`;
end#
I know It's been a while, but still...
I have this project I am working on, I have a table schema, see below
+--------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+------------+------+-----+---------+----------------+
| codeId | int(15) | NO | PRI | NULL | auto_increment |
| code | varchar(9) | YES | | NULL | |
| status | varchar(5) | YES | | 0 | |
+--------+------------+------+-----+---------+----------------+
This table is used for authorizations of codes, however some people send codes like dsfffMUBBDG345qwewqe for authorization, please note the capitalized part. In the code column there is a code MUBBDG345. I need to be able to check from the table if any combination of 9 characters the codes sent matches any of the codes in the db.
I have tried using this query but i just does not work.
select code, codeId, status from authCodes where 'dsfffMUBBDG345qwewqe' like code;
Is this even possible with a mysql query only?
you want to use
SELECT code, codeId, status
FROM authCodes
WHERE 'dsfffMUBBDG345qwewqe' LIKE CONCAT('%', code, '%')