I am using MySQL database. I want to insert data into it. But one column data contains special characters. (backslash) I want to replace it with Double backslash and then execute Insert query.
Can we do it using Insert Query?
While looking for the answer I came across
UPDATE your_table
SET your_field = REPLACE(your_field, '/', '//')
WHERE your_field LIKE '%articles/updates/%'
So it is possible use Replace in Update query.
Can we do the same in insert query? Please Let me know if you can help me.
You can use the following:
REPLACE INTO table_name(column_name1,column_name2,…)
VALUES(value1,value2,…)
More info from:
http://www.mysqltutorial.org/mysql-replace.aspx
You can use a BEFORE INSERT TRIGGER on your table
CREATE TRIGGER trigger_name
BEFORE INSERT
ON my_table
FOR EACH ROW
SET NEW.col_name = IF(NEW.col_name = '/','//',NEW.col_name);
Related
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 ??
I know that the UPDATE statement has the following format:
UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;
I was wondering whether there exists a format analogous to INSERT statement, something like this:
UPDATE table_name
SET (column1,column2,column3,...)
VALUES (value1,value2,value3,...)
WHERE some_column=some_value;
Is this a valid query?
I'm using MySQL.
The short answer - no.
All the variants of the update syntax have a set clause of the form colulmn1=..., column2=... etc. For a complete list of the supported syntax variants, you can check out the documentation.
I want to insert into a table where user id = to something
And change o to 1 which means user is online
Tried this
mysql_query("INSERT INTO `20s` VALUES('','','','','','','',1) WHERE `uid`='$user_id ");
But that doesn't get me anywhere. What's the right syntax?
Also what's the best way to keep a record of online friends in the database?
TIP:it's better to use update here
Correct syntax for insert query is:
INSERT INTO table_name
VALUES (value1, value2, value3,...)
but you need here is update query, so you may update already existing row:
UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value
You only need to change one column bit that show online so dont change other columns:
UPDATE `tableName` SET `columnForOnline`=1 WHERE user_id=online_user_id
There is no WHERE clause in INSERT statements (see MySQL documentation).
If you want to update a value in an existing row, use UPDATE:
UPDATE `20s` SET `online`=1 WHERE user_id=your_user_id
mysql_ functions are deprecated, use PDO or mysqli_ instead!
MySQL also supports REPLACE INTO, which follows the same syntax as INSERT. Be careful though, columns that you do not supply will be set to their defaults.
In your case:
REPLACE INTO `20s` VALUES('','','','','','','',1) WHERE `uid`= '$user_id'
(Plus, you are missing a closing quote ' at the end of your query)
Use update query instead of insert
UPDATE `tableName` SET `columnForOnline`=1 WHERE user_id=online_user_id
I have an extension I have made and inside this extension there is an "install.sql" file which contains various inserts.
Some of these inserts do it to tables which MAY exist or not depending on the clients site. What I would like to know is how to do for example something like:
"create table if exists" but for an "Insert Into".
This is an example of what im doing:
INSERT INTO `#__virtuemart_shipmentmethods_en_gb`
(virtuemart_shipmentmethod_id, shipment_name, shipment_desc, slug)
VALUES ((SELECT MAX(vs.virtuemart_shipmentmethod_id)
FROM `#__virtuemart_shipmentmethods` vs), 'Kiala','', 'kiala');
Thanks in advance for your help.
i don't know if i got your question right....
you want to insert somesthing and if its already there you want to update ?
if yes, you propably looking for THIS HERE otherwise your question is set up bad or its to late for me to get it right :)
INSERT INTO table (a,b,c) VALUES (1,2,3),(4,5,6)
ON DUPLICATE KEY UPDATE c=VALUES(a)+VALUES(b);
if you don't know if the table exists than you might should check all befor
usual i use pdo, don't know what you use but my solution would be some like this
$result = mysql_query("SHOW TABLES LIKE '#__virtuemart_shipmentmethods_en_gb'");
$tExists = mysql_num_rows($result) > 0;
if(!$tExists)
// Create Table Code here
Have you considered running this is a stored procedure? Something like
CREATE PROCEDURE `testprocdure` ()
BEGIN
IF (SELECT COUNT(*)
FROM information_schema.tables
WHERE table_schema = '[database name]'
AND table_name = '#__virtuemart_shipmentmethods_en_gb') = 1 THEN
INSERT INTO `#__virtuemart_shipmentmethods_en_gb` (virtuemart_shipmentmethod_id, shipment_name, shipment_desc, slug)
VALUES ((SELECT MAX(vs.virtuemart_shipmentmethod_id) FROM `#__virtuemart_shipmentmethods` vs), 'Kiala','', 'kiala');
ELSE
-- Handle if the table doesnt exists
SELECT ''; -- This is just so the SP wont return a error after the else. Remove this when you want to run it
END IF;
END
You could also make use of parameters if you have variable changing.
Then the create syntax should look like this
CREATE PROCEDURE `testprocdure` (IN p_TableName VARCHAR(100))
Change the name of the parameter and datatype to fit your needs.
Could this maybe help?
I want to write a trigger.
The trigger works in the following manner :
When table R_published gets a new entry, based on a column value in the entry(R_published.whichPublishable) it needs to copy a row from either the project_task_goodread_master table or the project_document_master table into the R_publishedGoodReads OR R_publishedDocuments tables respectively.
I have written the following trigger and I'm getting the error : "#1327 - Undeclared variable: R_publishedGoodReads"
CREATE TRIGGER trigger_after_published
AFTER INSERT ON R_published
FOR EACH ROW
BEGIN
IF (NEW.whichPublishable=1) THEN
SELECT * INTO R_publishedGoodReads FROM project_task_goodread_master
WHERE
goodReadID= new.publishedItemId;
ELSEIF (NEW.whichPublishable=2) THEN
SELECT * INTO R_publishedDocuments FROM project_document_master where
documentID=new.publishedItemId;
END IF
END
Is there anything wrong with the syntax ? Do I need to declare the table name that I am using for insert ? Thanks.
MySQL doesn't support SELECT...INTO TABLE. See MySQL Documentation
Try instead:
IF (NEW.whichPublishable=1) THEN
INSERT INTO R_publishedGoodReads (col1, col2...)
SELECT col1, col2... FROM project_task_goodread_master
WHERE goodReadID= new.publishedItemId;