MySql multiple delete statements - mysql

I couldn't find anything here or google that works for me.
I have tried the following code:
delete from wms where barcode = '65025351102908' and '105077351106577';
values are a varchar bear in mind.
No errors come up, but 0 rows are affected. But they are definitely out there.

Your WHERE clause isn't working as you expect. You can't factorise conditions such way
Writting this will only remove 65025351102908 :
WHERE barcode = '65025351102908' and '105077351106577';
-- ^------------------------^ ^---------------^
-- Condition 1 Condition 2 (always true because different than a falsy value)
I suppose you want to remove both 65025351102908 and 105077351106577
This is done using a OR (remove where the id is equal to the first one OR the second one)
Try this instead :
delete from wms where barcode = '65025351102908' or barcode = '105077351106577';
If you have a lot of barecode to remove, you can use the IN operator :
delete from wms where barcode IN ('65025351102908', '105077351106577');

Are you trying to delete the two rows with those two values?
Then do this:
delete from wms
where (barcode = '65025351102908') or (barcode = '105077351106577');
You can also do this:
delete from wms
where barcode in ('65025351102908','105077351106577');

If you want to delete "between" those values (taking in mind that you are writing them inside single quotes as string) your syntax could be better like this:
delete from wms where barcode BETWEEN '65025351102908' and '105077351106577';

Related

Run query on query results

I'm trying to clean all duplicate results on a phonebook i have, that means i'm trying to compare landline1 to landline2 and if they are equal (but not empty) i would like to either NULL or just replace with empty string one of them.
i use the following line to list all the matching results:
SELECT * FROM `csv_confirmed` WHERE landline1=landline2 AND landline1!="";
which gives me a full list, but it's too many to edit manually and i'm trying to automate it.
What would be the easiest way to run UPDATE (or anything else that might work here) to clear the "landline2" column of the results i found ?
Just change your query to an update:
UPDATE csv_confirmed
SET landline2 = NULL
WHERE landline1=landline2 AND landline1!=""

How do I remove an email domain value and add a new one in a column - mysql

So I have a bunch of users in a column that get refreshed as:
Bill#test.comXYZ
Tom#test.comXYZ
John#test.comXYZ
We refresh the database each week and I need to update these appropriate emails to:
Bill#domain.com
Tom#domain.com
John#domain.com
I figured I can use concat to do the latter, but I am stuck on the former issue. Is there a way to split the values (like split Bill#test.comXYZ into Bill - #test.comXYZ and then remove the #TEXT values?).
Anyways, any help will be much appreciated.
You can use the mySQL replace function, i.e.
UPDATE mytable
set myfield = replace (myfield, '#test.comXYZ', 'domain.com')
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_replace

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"});

Duplicate column and add an automatic extension with mySQL. How?

I have two columns with mySQL:
"part_no"
"pdf_link"
I need the "pdf_link" column to automatically grab/duplicate the "part_no" value and add a .pdf extension on the end.
For example: If part_no = 00-12345-998, then pdf_link = 00-12345-998.pdf
I need this to happen every time I insert.
I appreciate the help.
Erik
you can achive this effect by using triggers I think.
http://dev.mysql.com/doc/refman/5.0/en/trigger-syntax.html
CREATE TRIGGER ins_pdf AFTER INSERT ON MY_TABLE SET #pdf_link = concat(#part_no,'.pdf')
Why store this extra computed information in the database? You can do this in the query when you pull it out, or, if needed, you could make a view that does it only as-needed.
Example pseudo query (my brain hurts right now, so this is only an example):
select concat(`part_no`, ".pdf") as `pdf_link` from `parts`;
If you really need this, you could use a trigger to duplicate the data ans add the extra string.

MySQL add prefix to field table-wide

Basically I just decided to switch my primary ID to a "source" field, as I will be importing stuff from multiple sources. Now I'd like to make it clear where things come from, as such I'd like to add a prefix to it, as to be portalname:formerID. I've tried
UPDATE pics SET source='nk:'+source WHERE 1=1
UPDATE pics SET source='nk:'+source WHERE faces > 0 (matches all records)
but every time phpMyAdmin returns 0 row(s) affected. ( Query took 0.0056 sec )
Any idea?
Use CONCAT() ( http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_concat ) to concatinate strings, not "+".
you may try to omit the where clause altogether.
UPDATE pics SET source= concat('nk:',source )
or better yet, add a new column 'portal_name' and populate that seperately.