This one is braking my head . Moving old articles rating for client from old DB to new DB where the only reference between them is first word which is separated by dash in article title. I am able to pull the info I need by using SELECT but I cant figure out how to use the result to update the new table
table that needs to be updated
UPDATE
newDB.newtable.rating
SET newDB.newtable.rating.rating_count = oldvotes
the select that gives me the info on oldvotes
SELECT
oldvotes.votes AS oldvotes, old.title AS oldtitle,newtable.news_items.title as newtitle,newtable.news_items.id AS newID
FROM
oldDB.news_items AS old
INNER JOIN
oldDB.news_items.rating_count AS oldvotes
ON
oldvotes.article_id = old.id
INNER JOIN
newDB.newtable.news_items
ON
newDB.newtable.news_items.title
LIKE CONCAT
( '%', SUBSTRING_INDEX( old.title, '- ', 1 ) , '%' )
any help is appreciated!
If I understand correctly, you have in old.title something like thisisauniquekey-september-2012, and in news_items.title the value 'thisisauniquekey-somethingelse'.
You could select a key (a faster key than the title) and the oldvotes into a temporary table, say, oldratings, using the same query you run now:
SELECT news_items.keytobeusedonnewtable AS keyforrating, oldvotes.votes as oldvotes FROM etc.
Then you can run the update using oldratings:
UPDATE newDB.newtable.rating SET rating_value = oldvotes FROM
newDB.newtable.rating JOIN oldratings
ON rating.keyforrating = oldvotes.keyforrating;
Related
I am trying to update one column called 'Name' in one table using the naming conventions from another table.
My script below is not working and I am not quite sure why... Either get a syntax error or another way I tried to do this I ended up with all the 'Name' as NULL:
UPDATE table1
SET Name =
ISNULL(
(SELECT TOP 1 CorrectSSPname
FROM table2
WHERE UPPER(Name) LIKE '%’ + UPPER(WrongSSPname) + ‘%')
, Name
)
WHERE DATE >= '2018-07-01'
I can get 'like' to work using the following script for a single update but unable to do multi updates using the script above:
UPDATE table1
SET Name = 'xxx'
WHERE Name like 'yyy'
You can try a query like this:
UPDATE tabel1 tbl1
LEFT JOIN table2 tbl2 ON UPPER(tbl2.WrongSSPname) LIKE CONCAT('%', UPPER(tbl1.Name), '%')
SET tbl1.Name = tbl2.CorrectSSPname
WHERE tbl1.DATE >= '2018-07-01'
Modified some stuff from my pic so you guys can understand it
I have this database. I am trying to update a value from a table based on another value from an another table.
I want to update the SUM from salary like this :
( sum = presence * 5 )
This is what I've been trying to use ( unsuccessful )
update table salary
set suma.salary = users.presence * 5
FROM salary INNER JOIN users1 INNER JOIN presence on id_salary = id_presence
I am not sure what to do, I'd appreciate some help, Thanks
In MySQL to UPDATE tables with a join you use this syntax:
UPDATE table1, table2
SET table1.column = some expression
WHERE table1.column = table2.column
That said, even with the updated picture, in your SQL you are mentioning columns that I cannot understand in which table are to be found. You also have an inner join between salariu and users1, with no join condition. Could you please clean up the question and make everything clear?
Assuming you are making the updates to the db structure you were talking about, then you can start working on this one maybe:
UPDATE salary, presence
SET salary.sum = SUM(presence.hours) * 5
WHERE presence.id = salary.id
AND <some filter on the month that depends on salary.date>
Another way, but I'm not sure it is supported in all RDBMS, would be something like this:
UPDATE salary
SET sum = (
SELECT SUM(presence.hours) * 5
FROM user, presence
WHERE presence.id = salary.id
AND <some filter on the month that depends on salary.date>
)
I was trying to execute this statement to delete records from the F30026 table that followed the rules listed.. I'm able to run a select * from and a select count(*) from with that statement, but when running it with a delete it doesn't like it.. it gets lost on the 'a' that is to define F30026 as table a
delete from CRPDTA.F30026 a
where exists (
select b.IMLITM from CRPDTA.F4101 b
where a.IELITM=b.IMLITM
and substring(b.IMGLPT,1,2) not in ('FG','IN','RM'));
Thanks!
This looks like an inner join to me, see MySQL - DELETE Syntax
delete a from CRPDTA.F30026 as a
inner join CRPDTA.F4101 as b on a.IELITM = b.IMLITM
where substring(b.IMGLPT, 1, 2) not in ('FG', 'IN', 'RM')
Please note the alias syntax as a and as b.
Instead of the 'exists' function, you can match the id (like you do in the where clause):
delete from CRPDTA.F30026 a
where a.IELITM IN (
select b.IMLITM from CRPDTA.F4101 b
where a.IELITM=b.IMLITM
and substring(b.IMGLPT,1,2) not in ('FG','IN','RM'));
I believe this is what you really want, all IELITMs which meet your criteria.
delete from CRPDTA.F30026
where IELITM IN (
select IMLITM from CRPDTA.F4101
where substring(IMGLPT,1,2) not in ('FG','IN','RM'));
As my application is expanding, I now am changing the structure of my database; I now want to control file types within the database. I wanted to start with the current file types already in the database. My Database now has a [simplified] 2 table structure like:
tbFiles: pkFileID, fileType, fileName
tblFileType: pkFileType, typeName, typeDesc
I am trying to have the output of a SELECT query update into the newly created tblFileType table. I have tried among other things:
UPDATE tblFileType
INNER JOIN
(SELECT DISTINCT fileType FROM tblFiles) as X
SET typeName = fileType
but I always seem to get 0 row(s) affected.
When I run
SELECT DISTINCT fileType
FROM `tblFiles`
I get Showing rows 0 - 22 (~23 total, Query took 0.0074 sec)
I know this must be simple, but why is the UPDATE query not affecting 23 rows?
You need to add a JOIN condition like ON t1.fileType = x.fileType as follows:
UPDATE tblFileType t1
INNER JOIN
(
SELECT DISTINCT fileType
FROM tblFiles
)as X ON t1.fileType = x.fileType
SET t1.typeName = X.fileType
Update: Since the table tblFileType is blank, you will need to use INSERT something like:
INSERT INTO tblFileType(typeName )
SELECT DISTINCT fileType
FROM tblFiles
WHERE -- a condition here
you just want to populate the table - not update anything in there (especially since nothing exists yet)
INSERT INTO tblFileType(typeName )
SELECT DISTINCT fileType FROM tblFiles
I have the database of ATM card in which there are fields account_no,card_no,is_blocked,is_activated,issue_date
Fields account number and card numbers are not unique as old card will be expired and marked as is_block=Y and another record with same card number ,account number will be inserted into new row with is_blocked=N . Now i need to update is_blocked/is_activated with help of issue_date i.e
UPDATE card_info set is_blocked='Y' where card_no='6396163270002509'
AND opening_date=(SELECT MAX(opening_date) FROM card_info WHERE card_no='6396163270002509')
but is doesn't allow me to do so
it throws following error
1093 - You can't specify target table 'card_info' for update in FROM clause
Try this instead:
UPDATE card_info ci
INNER JOIN
(
SELECT card_no, MAX(opening_date) MaxOpeningDate
FROM card_info
GROUP BY card_no
) cm ON ci.card_no = cm.card_no AND ci.opening_date = cm.MaxOpeningDate
SET ci.is_blocked='Y'
WHERE ci.card_no = '6396163270002509'
That's one of those stupid limitations of the MySQL parser. The usual way to solve this is to use a JOIN query as Mahmoud has shown.
The (at least to me) surprising part is that it really seems a parser problem, not a problem of the engine itself because if you wrap the sub-select into a derived table, this does work:
UPDATE card_info
SET is_blocked='Y'
WHERE card_no = '6396163270002509'
AND opening_date = ( select max_date
from (
SELECT MAX(opening_date) as_max_date
FROM card_info
WHERE card_no='6396163270002509') t
)