updating 2 fields in mysql (with one update being null update) - mysql

I'm basically trying to do this:
update <table> set process_flg='N' where id<1201;
update <table> set time_stamp=null where id<1201;
this works fine if I use 2 different updates.
but, if I compress them to be:
update <table> set process_flg='N' and time_stamp=null where id<1201;
it just doesn't work.
the time_stamp remains the value it was while process_flg becomes 0.

I see your your compressed query and there is mistake. Your compressed query should be like below:
UPDATE SET process_flg='N', time_stamp=null WHERE id<1201;
You need to use "," instead of "and" statement.
Please take a look here more information:
http://www.w3schools.com/sql/sql_update.asp

Related

MySql multiple delete statements

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';

SQL Refer to Column on Right Side of SET Statement

Is the following valid in SQL (theoretical)?
UPDATE Classes
SET bore = bore * 2.5;
I can't find a source that says whether this is valid or not.
This should multiply every entry in the bore attribute by 2.5. If not, is there a way to multiply a column by a scalar and update?
The SQL is correct, see the SQL fiddle example on the link below. It creates the table, inserts some values and updates the table with your SQL.
See this SQL fiddle for example
It's valid and correct. You're assigning to each row of bore column a new value, multiplying the current value by 2.5. This is perfectly fine in the UPDATE statement.
The MySQL documentation has a similar example:
If you access a column from the table to be updated in an expression,
UPDATE uses the current value of the column. For example, the following statement sets col1 > to one more than its current value:
UPDATE t1 SET col1 = col1 + 1;
You can then suppose that this is valid.

SQL Update returning zero rows

UPDATE starfsfolk
SET starfsfolk.stada=2
WHERE starfsfolk.deild LIKE '%Hugbúnaðardeild%';
UPDATE starfsfolk
SET starfsfolk.stada=3
WHERE starfsfolk.deild LIKE '%Markaðsdeild%'
is prescisely the code i'm using.
i've tried various different versions of it(like = "Markaðsdeild" or LIKE "Markaðsdeild")
most of which work if i'm using select, but i needed to use update and its not working for some reason.
This WHERE command works on select but
it returns zero rows if i'm using the update command. What am i doing wrong?
Edit:
Just to clarify, stada is set to 1 in all cases before and after the update command. it hasn't changed from 1 to 2 and 3 like i wanted it to.
Edit2: heres a screenshot of the database, i could also give you the create commands.
Edit3: Stada is bit, not int, i found out, not sure what that changes tho.
Solution: Solved myself, since bit is just 1 and 0, the error was in the creation of the table so i remade it with stada as int and now the code is working.
If the value of stada is not changed by the query then zero rows will be returned because nothing was updated.
The character sets of the server and of the client are different.
http://dev.mysql.com/doc/refman/5.5/en/charset-connection.html

Update MySQL Column with WHERE condition

I need to change the information in a column to Degraded, where it is Feature Broken or Degradated. Is this the correct line?
UPDATE tablename
SET column = 'Degraded'
WHERE column = 'Degradated' OR column= 'Feature Broken'
Thanks
You may want to use an IN operator, to avoid the OR:
UPDATE tablename SET columnname = 'Degraded' WHERE columnname IN ('Degradated', 'Feature Broken');
Also, I would suggest running a SELECT first so you are somewhat aware of how many rows will be changed with your command.
Yes, it is.
You could make it more concise (but it is exactly the same otherwise)
...
WHERE column IN ('Degradated','Feature Broken')

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.