MySQL add prefix to field table-wide - mysql

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.

Related

How can I update all records in an Inventory Table to add "/invent" to the beginning of an existing file path using a single query?

I want to add "/invent" at the beginning of the file path in the invImage and invThumbnail columns.
This is what I have in mind,
SELECT FROM inventory
SELECT CONCAT("/invent")
AS invImage, invThumbnail;
Since it is not easy to undo mistakes in SQL, a confirmation of my potential solution will be helpful.
Your current version will overwrite the value in the invImage column, but your stated goal is to
a) prepend the string to the existing value, and
b) do this in two columns, not just one
WHERE invImage = "/images"; also won't match any of the rows, because none of them contain exactly that value. I'll assume you want to update all rows whose invImage value starts with "/images".
Therefore, try this:
UPDATE
inventory
SET
invImage = CONCAT("/invent", invImage),
invThumbnail = CONCAT("/invent", invThumbnail),
WHERE
invImage LIKE "/images%";
My suggestion: add temporarily two columns to the table: tmp_invImage and tmp_invThumbnail.
Then run the query:
UPDATE table SET
tmp_invImage = CONCAT("/invent",invImage),
tmp_invThumbnail = CONCAT("/invent",invThumbnail)
After the update, look if the values of tmp_invImage and tmp_invThumbnail are correct.
Then update the original columns:
UPDATE table SET
invImage = tmp_invImage),
invThumbnail = tmp_invThumbnail
and delete the tmp_ columns.

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

Update MySQL without specifying column names

I want to update a mysql row, but I do not want to specify all the column names.
The table has 9 rows and I always want to update the last 7 rows in the right order.
These are the Fields
id
projectid
fangate
home
thanks
overview
winner
modules.wallPost
modules.overviewParticipant
Is there any way I can update the last few records without specifying their names?
With an INSERT statement this can be done pretty easily by doing this:
INSERT INTO `settings`
VALUES (NULL, ...field values...)
So I was hoping I could do something like this:
UPDATE `settings`
VALUES (NULL, ...field values...)
WHERE ...statement...
But unfortunately that doesn't work.
If the two first columns make up the primary key (or a unique index) you could use replace
So basically instead of writing
UPDATE settings
SET fangate = $fangate,
home = $home,
thanks = $thanks
overview = $overview,
winner = $winner,
modules.wallPost = $modules.wallPost,
modules.overviewParticipant = $modules.overviewParticipant
WHERE id = $id AND procjectId = $projectId
You will write
REPLACE INTO settings
VALUES ($id,
$projectId,
$fangate,
$home,
$thanks
$overview,
$winner,
$modules.wallPost,
$modules.overviewParticipant)
Of course this only works if the row already exist, otherwise it will be created. Also, it will cause a DELETE and an INSERT behind the scene, if that matters.
You can't. You always have to specify the column names, because UPDATE doesn't edit a whole row, it edits specified columns.
Here's a link with the UPDATE syntax:
http://dev.mysql.com/doc/refman/5.0/en/update.html
No, it works on the INSERT because even if you didn't specify the column name but you have supplied all values in the VALUE clause. Now, in UPDATE, you need to specify which column name will the value be associated.
UPDATE syntax requires the column names that will be modified.
Are you always updating the same table and columns?
In that case one way would be to define a stored procedure in your schema.
That way you could just do:
CALL update_settings(id, projectid, values_of_last_7 ..);
Although you would have to create the procedure, check the Mysql web pages for how to do this, eg:
http://docs.oracle.com/cd/E17952_01/refman-5.0-en/create-procedure.html
I'm afraid you can't afford not specifying the column names.
You can refer to the update documentation here.

SQL server insert multiple rows and incrementing a int column

I have some rows in a table and need to transfer them to another table. In the destination table i need also to add a field with an incremental value.
I'm doing the following, but i know that something in the insert is wrong, because the incremented value (intCodInterno) is always the same:
INSERT INTO Emp_VISOT.dbo.TBL_GCE_ARTIGOS
( strCodigo ,
strDescricao ,
intCodInterno ,
intCodTaxaIvaCompra ,
intCodTaxaIvaVenda ,
strCodCategoria ,
strAbrevMedStk ,
strAbrevMedVnd ,
strAbrevMedCmp ,
bitAfectaIntrastat
)(
SELECT A.Artigo ,
a.Descricao ,
IDENT_CURRENT('Emp_VISOT.dbo.TBL_GCE_ARTIGOS')+1,
'3' ,
'3' ,
'1' ,
'Un' ,
'Un' ,
'Un' ,
'0'
FROM PRIVESAM.DBO.Artigo A)
What do i need to change so the value is incremented correcty?
Thank you.
EDIT:
I made a small change in the query, and now it works.
I just insert a SELECT in the IDENT_CURRENT inside brackets:
(SELECT IDENT_CURRENT('Emp_VISOT.dbo.TBL_GCE_ARTIGOS')+1)
I got all the rows that i need from the old table to the new with the incremented value.
the IDENT_CURRENT('Emp_VISOT.dbo.TBL_GCE_ARTIGOS')+1
evaluated once when you want to run the query and all the rows will get the same id.
first solution is to iterate over the select result by a loop construct like cursor or whatsoever and insert the incremented index(you do that)
second solution is to make that column in destination table identity
Remove the part with intCodInterno and in SQL Server use the Identity property to automatically increment it for you.
IDENT_CURRENT won't update until the transaction commits, therefore its value remains constant until you insert.
Here are three options for fixing this issue:
Use some kind of counter (#newRowNum) such that for each row in your SELECT query, #newRowNum = #newRowNum +1, and thus your intCodInterno number = IDENT_CURRENT() + #newRowNum. This would probably require a lot of hacking to work though. Don't recommend it.
Insert your rows sequentially using the same business logic you have now - it will be tremendously less performant, however. Don't recommend it.
Set that column in your destination table to be an identity column itself. This is by far the best way to do it.
If you need a custom identity function (I assume there's a reason you're not using an identity column now), you can create one using some of the steps outlined above: http://www.sqlteam.com/article/custom-auto-generated-sequences-with-sql-server
In my case , i Inserted rows sequentially using the same business logic. I cannot use auto increment as i have to import old data also into this column. Once you have imported the data then u may go for updating the column for auto increment .

Does replace into have a where clause?

I'm writing an application and I'm using MySQL as DBMS, we are downloading property offers and there were some performance issues. The old architecture looked like this:
A property is updated. If the number of affected rows is not 1, then the update is not considered successful, elseway the update query solves our problem.
If the update was not successful, and the number of affected rows is more than 1, we have duplicates and we delete all of them. After we deleted duplicates if needed if the update was not successful, an insert happens. This architecture was working well, but there were some speed issues, because properties are deleted if they were not updated for 15 days.
Theoretically the main problem is deleting properties, because some properties are alive for months and the indexes are very far from each other (we are talking about 500, 000+ properties).
Our host told me to use replace into instead of deleting properties and all deprecated properties should be considered as DEAD. I've done this, but problems started to occur because of syntax error and I couldn't find anywhere an example of replace into with a where clause (I'd like to replace a DEAD property with the new property instead of deleting the old property and insert a new to assure optimization). My query looked like this:
replace into table_name(column1, ..., columnn) values(value1, ..., valuen) where ID = idValue
Of course, I've calculated idValue and handled everything but I had a syntax error. I would like to know if I'm wrong and there is a where clause for replace into.
I've found an alternative solution, which is even better than replace into (using simply an update query) because deletes are happening behind the curtains if I use replace into, but I would like to know if I'm wrong when I say that replace into doesn't have a where clause. For more reference, see this link:
http://dev.mysql.com/doc/refman/5.0/en/replace.html
Thank you for your answers in advance,
Lajos Árpád
I can see that you have solved your problem, but to answer your original question:
REPLACE INTO does not have a WHERE clause.
The REPLACE INTO syntax works exactly like INSERT INTO except that any old rows with the same primary or unique key is automaticly deleted before the new row is inserted.
This means that instead of a WHERE clause, you should add the primary key to the values beeing replaced to limit your update.
REPLACE INTO myTable (
myPrimaryKey,
myColumn1,
myColumn2
) VALUES (
100,
'value1',
'value2'
);
...will provide the same result as...
UPDATE myTable
SET myColumn1 = 'value1', myColumn2 = 'value2'
WHERE myPrimaryKey = 100;
...or more exactly:
DELETE FROM myTable WHERE myPrimaryKey = 100;
INSERT INTO myTable(
myPrimaryKey,
myColumn1,
myColumn2
) VALUES (
100,
'value1',
'value2'
);
In your documentation link, they show three alternate forms of the replace command. Even though elided, the only one that can accept a where clause is the third form with the trailing select.
replace seems like overkill relative to update if I am understanding your task properly.