Unable to delete from a table - mysql

Here is a piece of mysql code
DELETE FROM tbl_node_details WHERE ParentId IN (1,2,3,4,5);
This works fine and all rows are deleted with the respective ids.
But when is do like this
SET #a='1,2,3,4,5'
DELETE FROM tbl_node_details WHERE ParentId IN (#a);
it doesn't work...y is it so??

You cannot use parameters with in statement. Try to loop or build string wo parameters

Cannot use parameters within query statements. Try using loops.

As #hkutluay stated this won't work.
Assuming you get your parameters from another table you can do :
DELETE FROM tbl_node_details WHERE ParentId IN (SELECT <id_col_name> FROM <other_table> WHERE <condition>);

Related

MySql Backslashes disappear when data is updated to another table

I have a table that contains directory paths and the data looks like this:
But when I run an update statement where I join another table to this one and update the to existing rows in the new table, the backslashes disappear like this:
This is the update statement, where
"MIJob.SourceFile"
has the proper text containing the backslashes, and
"MIJobFileLocation.Path_Folder"
is the column being updated and does not have backslashes in its data.
This is the update statement:
UPDATE MIJobFileLocation
INNER JOIN MIJob
ON MIJobFileLocation.MIJobFileLocationGUID = MIJob.MIJobFileLocationGUID_Source
SET
MIJobFileLocation.Path_Folder = MIJob.SourceFile
WHERE MIJob.SourceFile IS NOT NULL
This SQL will run in a stored procedure in MySQL. How can I preserve the backslashes?
I’ve been googling this for hours with no success.
Thank you.
Execute this line before your update statement. Seems like a hack solution, but it works.
SET SESSION sql_mode='NO_BACKSLASH_ESCAPES'
UPDATE sometable
set.....

how to delete single record from list

I have a record in table column like this (1001,1002,1003,1004,1005)
and I want to delete "1003" from this list. Please help me.
For your own good, don't store data like this. This type of issue is not, by far, the biggest problem you will run into.
This being said, you can solve you issue by using:
UPDATE TABLE
SET COLUMN = REPLACE (COLUMN, ',1003,', ',')
WHERE ID = PK;
even this will work:
update tablename set column=(select
substr(column,1,instr(colname,',',2))||susbtr(column,instr(column,',',3),length(column)-
instr(column,',',3)) from tablename where id=value;

MySQL REPLACE variable string

I want to use MySQL query to change a link .
the link is like this :
http://website.com/click.php?ad_client=blablabla&add_id=548124&more=stuffhere
if I know the add_id number this is easy :
UPDATE table SET name = REPLACE(name, '&add_id=548124', '')
The problem is I have to change 5000 lines and I don't know the add_id number ... so what would be a correct mysql replace() code to remove &add_id=somenumber ??
USE This....
UPDATE table
SET name = CONCAT(SUBSTRING(name , 1,
INSTR(name ,'&add_id') - 1),SUBSTRING(name ,
INSTR(name , '&more'),
LENGTH(name ) - INSTR(name , '&add_id')))
Either you can do it via UDF - SO Answer or you can simply write PHP code which will replace value & update column again in table.
I would create a stored procedure that uses a cursor to iterate over each row that needs updating.
In the procedure I would find the link to replace and then replace them, one by one.
I've made a sqlfiddle to show how you can get the part to replace inside an select.
I think this approach is clean and easy to read but it's possible to write this a update (that will most likely be hard to read).
first to see that it works :
SELECT 'http://website.com/click.php?ad_client=blablabla&add_id=548124&more=stuffhere' INTO #link;
SELECT
#link as full_link,
SUBSTR(#link,LOCATE('&',#link),LOCATE('&',#link,LOCATE('&',#link)+1)-LOCATE('&',#link)) as remove_part,
REPLACE(#link,SUBSTR(#link,LOCATE('&',#link),LOCATE('&',#link,LOCATE('&',#link)+1)-LOCATE('&',#link)),'') as final_link
And now for your UPDATE:
UPDATE table SET name = REPLACE(name,SUBSTR(name,LOCATE('&',name),LOCATE('&',name,LOCATE('&',name)+1)-LOCATE('&',name)),'')
try this with REPLACE
UPDATE Table1
SET name = REPLACE(if(name like '%add_id=%','' , name ),
'&add_id=' , '' )
DEMO HERE

DBIx::Class update only one row

I am using DBIx::Class and I would like to only update one row in my table. Currently this is how I do it:
my $session = my_app->model("DB::Session")->find(1);
$session->update({done_yn=>'y',end_time=>\'NOW()'});
It works, but the problem is that when it does find to find the row, it does this whole query:
SELECT me.id, me.project_id, me.user_id, me.start_time, me.end_time, me.notes, me.done_yn FROM sessions me WHERE ( me.id = ? ): '8'
Which seems a bit much when all I want to do is update a row. Is there anyway to update a row without having to pull the whole row out of the database first? Something like this is what I am looking for:
my_app->model("DB::Session")->update({done_yn=>'y',end_time=>\'NOW()'},{id=>$id});
Where $id is the WHERE id=? part of the query. Does anyone know how to do this? Thanks!
You can run update on a restricted resultset which only matches this single row:
my_app->model("DB::Session")->search_rs({ id=> 1 })->update({done_yn=>'y',end_time=>\'NOW()'});
I suggest you use a DateTime->now object instead of literal SQL for updating the end_time column because it uses the apps servers date and time instead of the database servers and makes your schema more compatible with different RDBMSes.
Do you have a check if the row was found to prevent an error in case it wasn't?
You might want to use update_or_create instead.
You could use the "columns" attribute:
my $session = my_app->model("DB::Session")->find(1, {columns => "id"});

add the same thing to a table column by sql command

there is a table named test. in it. there are some columns as this:
001.jpg
...
999.jpg
now i want to use a sql command to add a url before them. as this http://www.example.com/xxx/001.jpg.....is there a way to get this? thank you.
There is two way to accomplish this task. It depends up to you that what you want?
If you want to add a url in the database permanently then you have to use update query with no any where condition, Although if you want to only show your field with this added url you have to use select query.
Please find below the examples for both:
Suppose that your table column name is imageName then UPDATE query will be
UPDATE test SET imageName = CONCAT("http://www.example.com/xxx/", imageName);
And the SELECT query will be
SELECT CONCAT("http://www.example.com/xxx/", imageName) FROM test;
Supposing that your field is called url, a simple UPDATE query will do:
UPDATE test SET url = CONCAT("http://", url);