update all rows where there is a fk constraint issue - mysql

I'm migrating a table into a new database. There is a fk constraint problem because there was never a relationship set between the 2 tables (tbl_contacts.contact_ID and tbl_communications.contact_ID which is the fk). I can see all the rows with
SELECT * FROM Farm.tbl_communication as S
LEFT JOIN Farm.tbl_contacts ON S.contact_ID = Farm.tbl_contacts.contact_ID
WHERE Farm.tbl_contacts.contact_ID IS NULL
I was just trying to delete those rows and I tried this:
DELETE FROM Farm.tbl_communication
WHERE Farm.tbl_communication.contact_ID (SELECT contact_ID
FROM ( SELECT * FROM Farm.tbl_communication) as S
LEFT JOIN Farm.tbl_contacts ON S.contact_ID = Farm.tbl_contacts.contact_ID
WHERE Farm.tbl_contacts.contact_ID IS NULL)
Which did not work. I have now thought better of it and I'm going to create a catch-all contact so I'll update all the communications.contact_ID with the new catch-all id. The problem is I have no idea how to go about that. what would the syntax be?

DELETE FROM Farm.tbl_communication
WHERE NOT EXISTS (
SELECT 1
FROM Farm.tbl_contacts
WHERE Farm.tbl_contacts.contact_ID = Farm.tbl_communication.contact_ID);
...or you can update them by changing the DELETE bit to UPDATE.

Related

Is it possible to delete multiple rows in MySQL if target table is in from clause?

I'm using PHPMYAdmin and MySQL Database. I would like to remove from the table 1masterinventory all the rows (PK RID) that are not in 1amazoninventory (PK RID). I feel like this query should work but I get the error "#1093 - You can't specify target table '1masterinventory' for update in FROM clause."
DELETE FROM 1masterinventory
WHERE rid IN
(
SELECT rid
FROM 1masterinventory
WHERE rid NOT IN
(
SELECT rid FROM
1amazoninventory WHERE 1
)
)
SQL Fiddle:
DELETE m
FROM 1masterinventory m
LEFT JOIN 1amazoninventory a ON a.rid = m.rid
WHERE a.rid IS NULL;

Using var into an insert update request does not work

I try to insert values into a table using following request :
INSERT IGNORE itemmaster
(
SKU,
product_id
)
SELECT
#massimport_SKU := main.SKU,
#massimport_product_id := prd.entity_id
FROM itemmaster AS main
INNER JOIN product_entity AS prd ON prd.sku = main.SKU
ON DUPLICATE KEY UPDATE product_id = #massimport_product_id
SKU is a unique key.
The problem is the value of product_id is always the same id. If I only execute the select, product_id are different but after insert, only one value in product_id column. I think this is a problem with var #massimport_product_id cause if I useON DUPLICATE KEY UPDATE product_id = prd.entity_id instead the request work perfectly.
But cause it's an automatic generated request who work well in all other case, I hope somebody coul me explain why this append.
Thx
Don't use session variables, that can't work.
Use this:
INSERT IGNORE itemmaster( SKU, product_id )
SELECT main.SKU, prd.entity_id
FROM itemmaster AS main
INNER JOIN product_entity AS prd ON prd.sku = main.SKU
ON DUPLICATE KEY UPDATE product_id = prd.entity_id
;
Demo --> http://www.sqlfiddle.com/#!2/592187/1
However INNER JOIN in this query always returns records (values of SKU column), that already exist in itemmaster table, so INSERT is useles in this case, and the same can be done simpler, using multitable update:
UPDATE itemmaster main
JOIN product_entity AS prd ON prd.sku = main.SKU
SET main.product_id = prd.entity_id;
Demo: http://www.sqlfiddle.com/#!2/762eb4/1

Error #1093 - You can't specify target table 'relProductsPrices' for update in FROM clause

I'm upgrading and optimizing an old table structure.
In order to properly work with replace into, I'm removing old zombie entries that interfer with the new unique key over 2 columns.
Query:
DELETE from `relProductsPrices` where `ID` in
(SELECT scanA.ID from `relProductsPrices` as scanA
inner join `relProductsPrices` as scanB
where scanA.ID < scanB.ID
and scanA.product = scanB.product
and scanA.priceName = scanB.priceName);
Error:
#1093 - You can't specify target table 'relProductsPrices' for update in FROM clause
I'm not sure how to get this into one mySQL Query properly, at this time?
I hope this question is no duplicate entry, I seemed unable to find a similar, adaptable entry. There are questions regarding this error, but I'm not having an update query here at all, and the solution most people state (create a subselect) was already done by me beforehand already.
Thanks in advance!
Try this:
DELETE FROM `relProductsPrices`
WHERE `ID` IN (
SELECT
tmp.ID
FROM (
SELECT
scanA.ID
FROM
`relProductsPrices` as scanA
INNER JOIN `relProductsPrices` as scanB
ON scanA.ID < scanB.ID
AND scanA.product = scanB.product
AND scanA.priceName = scanB.priceName
) as tmp
);

Update new table with specific select from other table MySql

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;

mysql 3 table join with update

I am trying to update the status of a column, by checking two joined tables. Even though I dont get an error. The colum is not updating. I want to take the general blockplot id and see if there is a transaction that matches and or a a container. If there is a transaction but no container i need to mark it as P.
UPDATE (general
LEFT JOIN
transactions
ON
general.blockplotid=transactions.blockplotid)
LEFT JOIN
container
ON
general.blockplotid=container.blockplotid
SET general.lotstatus = 'P'
WHERE general.lotstatus != 'U' AND
transactions.id_transaction IS NOT NULL AND
container.id_container IS NULL
So summarize, I have 3 tables. I only want to update one colum in one table. I want to check for values in the other two tables, their values depend upon the set value. The three tables are connected with a primary key to foreign key.
When I do a double join select statement. The query seems correct.
SELECT transactions.blockplotid AS blockplotid_2, container.blockplotid AS blockplotid_1, general.blockplotid, general.lotstatus, container.id_container, transactions.id_transaction
FROM ((general LEFT JOIN transactions ON general.blockplotid=transactions.blockplotid) LEFT JOIN container ON general.blockplotid=container.blockplotid)
ORDER BY general.blockplotid ASC
However it seems as though the join for the update isnt like the select.
This query seemed to work:
This query worked:
UPDATE ((general LEFT JOIN transactions ON transactions.blockplotid=general.blockplotid) LEFT JOIN container ON container.blockplotid=general.blockplotid)
SET general.lotstatus='P'
WHERE general.blockplotid!='U' AND container.id_container is null AND transactions.id_transaction is not null
The difference here is the case of IS NOT NULL and also the order of the where condition.
Is there any explanation for this?
I suspect the parentheses/locations of the joins. Try a setup like this:
UPDATE
Table1
SET
Table1
. Field1 = StagingTable . Field1
FROM
Table1
INNER JOIN StagingTable
ON Table1 . Field2 = StagingTable . Field2
WHERE
StagingTable . Field3 IS NOT NULL
I would suggest restructuring your UPDATE statement to have an embedded SUB-SELECT. update general set lotstatus = (SELECT .... WHERE .... )