I've got two columns in the same table for my users: name-displayed and short-name.
name-displayed is populated with the full name of the user, for example "John Doe". In short-name, there is the short value, e.g. "john-doe" (essentially de-capitalized and hyphenated).
How would I amend the data in short-name based on the data in name-displayed? I'm sure I could use a self-join based on UPDATE, but I'm not sure how to implement a change in data across the columns.
Any help would be hugely appreciated!
You need to use the Lower and Replace functions for this.
See: Lower and Replace in the docs.
Update <table_name>
set `short-name` = REPLACE(LOWER(`name-displayed`), ' ','-')
where <conditions>;
In case you want this done automatically, you'll need to write a trigger as Walter_Ritzel suggests.
delimiter //
CREATE TRIGGER auto_set_short_name BEFORE INSERT ON account
FOR EACH ROW
BEGIN
SET NEW.`short-name` = REPLACE(LOWER(`name-displayed`), ' ','-');
END;//
delimiter ;
You could use triggers: Triggers
A trigger Before Insert/Update could solve that easily.
delimiter //
CREATE TRIGGER ins_sum BEFORE INSERT OR UPDATE ON table
FOR EACH ROW
begin
SET New.`short-name` = REPLACE(LOWER(NEW.`name-displayed`), ' ','-') ;
end;
//
Use backticks or this char: ```, to make sure the - is not interpreted as a minus sign.
update table a join table b on a.id = b.id
set a.short-name = b.name-displayed;
I understand you need to set name-displayed same as short-name,
if that is not the case
care to explain.
what you want to amend ??
Related
I want to automatically copy data from one column to another in one table.
The following query does this, but not automatically:
UPDATE table SET columnB = columnA
Could someone show a trigger for this (that does it automatically)?
Note: columnB and columnA are integers.
Should be as simple as:
delimiter //
CREATE TRIGGER before_table_update BEFORE UPDATE ON table
FOR EACH ROW
BEGIN
SET new.columnB = new.columnA;
END //
delimiter ;
Be aware, this will only effect UPDATE's. You will need to copy this trigger for inserts as well before_table_insert BEFORE INSERT...
I have been trying to create a Trigger, however my attempts have been unsuccessful. I seem to be getting an error (#1064), which I have no solution for. Can somebody explain or demonstrate any faults in the syntax.
Let me specify:
I have delivery_id as primary key in delivery table,
I also have delivery_id as a foreign key in entry_log table.
By comparing both id's(if true), will return a text referring to the output of the bit (either 0 or 1)
DELIMITER //
DROP TRIGGER IF EXISTS entry_trigger//
CREATE TRIGGER entry_trigger BEFORE INSERT ON entry_log
FOR EACH ROW
BEGIN
DECLARE #xentry VARCHAR(45)
DECLARE #inta bit
SET #inta = SELECT allowed
FROM delivery
WHERE delivery.delivery_id = entry_log.delivery_id;
CASE
when #inta = 0 then #xentry = 'Acces Denied'
when #inta = 1 then #xentry = 'Acces Allowed'
END CASE
INSERT INTO entry_log(entry_time,access_allowed) VALUES(now(),#xentry);
END
//
This is assuming that you use MySQL. In the body of the trigger you use
WHERE delivery.delivery_id = entry_log.delivery_id;
I think you want to compare to the entry_log entry that the trigger is running on, right? In that case you must use this syntax:
WHERE delivery.delivery_id = NEW.delivery_id;
see here for more examples.
UPDATE
I see that also you try to do an INSERT INTO entry_log within the TRIGGER. This will of course not work, because you would create an infinite recursive loop. Within the
body of the trigger you can do unrelated table access, but not into the table you are inserting. You can change the values to be inserted by the trigger by setting NEW.xyz = whatever
UPDATE 2
I doubt, that your CASE statement is correct. At least it must end with END CASE. You can use IF here, since you don't have many cases to address. If you must use CASE this post might help you: MYSQL Trigger set datetime value using case statement
UPDATE 3
I am not sure, but I think you need brackets around the variable setting statement. try this trigger definition:
DELIMITER //
DROP TRIGGER IF EXISTS entry_trigger//
CREATE TRIGGER entry_trigger BEFORE INSERT ON entry_log
FOR EACH ROW
BEGIN
SET #inta = (SELECT allowed
FROM delivery
WHERE delivery.delivery_id = NEW.delivery_id);
SET NEW.access_allowed = #inta;
SET NEW.entry_time = NOW();
END
//
Note, that this is written out of my head, so beware of syntax errors in my script.
Just learning about triggers and I'm created the following trigger;
CREATE TRIGGER `incremental_before_ins_tr` BEFORE INSERT ON incremental`
FOR EACH ROW
BEGIN
SET NEW.source = (Select source from crm_record
where msisdn = new.msisdn order by dat DESC limit 1);
END;
However the value does not appear to be getting updated. Any ideas?
I've actually managed to solve this myself. Here is the updated code
CREATE TRIGGER `incremental_before_ins_tr` BEFORE INSERT ON `incremental`
FOR EACH ROW
BEGIN
SET NEW.source = (Select source from crm_record
where crm_record.msisdn = new.msisdn order by dat DESC limit 1);
END;
I needed to specify the table name prior to the column value on line 5.
Looks like you have a typo.
You have entered incremental with a trailing backtick instead of enclosing it in backticks.
It is likely that your trigger is now bound to a table called incremental` which I am assuming does not exist.
Since you have ruled out the above. I see that the UPDATE keyword is missing. Add UPDATE table before your SET line. Replace table with the name of your table.
I've got a mysql table column that acts as a counter (int). Each time I update that column I want the field value to get plussed with 1.
So if it was 45 I want it to be 46.
How could I do that with SQL?
You can use a query such as this one :
update your_table
set your_column = your_column + 1
where identifier = X
Of course, up to you to replace the names of the table and the columns ;-)
And to make sure the condition in the where clause is OK ;-)
MySQL v5.0.2 or higher supports triggers, which are pieces of code that are executed, whenever an insert/update/delete operation is made on one of the rows.
for more information about triggers in MySQL check this link
Since you want it to happen each time, you could use a trigger:
delimiter //
create trigger increment_field before update on your_table
for each row
begin
if new.your_column <> old.your_column then
set new.your_column = new.your_column + 1
end if;
end;//
delimiter ;
I am not entirely sure I understand your question. The above solution will make it so whenever a row is updated such that your_column is given a different value, it will instead make your_column = new_value+1. The use of new and old keywords and the conditional used on each row should be changed to suit your needs.
is there any way i can group several of my columns together?
in one for my scripts i need to set several of my columns to the same value, is there a better way than typing out all the column names in my update query?
thanks
If these columns MUST always have the same value (that seems pointless) you could set an UPDATE TRIGGER on the column
delimiter |
CREATE TRIGGER somename BEFORE INSERT ON table1
FOR EACH ROW BEGIN
SET table1.col2=NEW.col1, table1.col3=NEW.col1;
END;
delimiter ;
But if that was the case, it would really make sense to just use ONE column instead of three.
Or, if its a Rights issue (this user shouldn't have the permission to make the columns different) you could build a Stored Procedure for doing the update
delimiter |
CREATE STORED PROCEDURE somename(IN val INT,IN whereval INT)
BEGIN
UPDATE table1 SET table1.col1=val, table1.col2=val, table1.col3=val
WHERE table1.id=whereval;
END;
delimiter ;
You'll need to type each column's name, there is no workaround.
UPDATE mytable
SET col1 = 1,
col2 = 1,
…
No, this is not possible.
See the syntax for update:
...
SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}
...
Update syntax for MySQL requires each column to be named explicitly
update table set a = 'value', b = 'value'