Creating an update statement from a nested select - mysql

I have a mysql statement below
select c11 as genre, t3.strGenre, t2.idGenre t2idGenre, t1.idMVideo
from musicvideo t1
join genrelinkmusicvideo t2
on t2.idMVideo=t1.idMVideo
join genre t3
on t3.idGenre=t2.idGenre
where not (c11=strGenre) and genre='Alternative Rock'
This works fine and gives me the desired results.
Now when I want to put this into an update statement i get stuck.
update t2
set idGenre=62
where (select c11, t3.strGenre, t2.idGenre t2idGenre
from musicvideo t1
join genrelinkmusicvideo t2
on t2.idMVideo=t1.idMVideo
join genre t3
on t3.idGenre=t2.idGenre
where not (c11=strGenre) and genre='Alternative Rock')
I know this is not correct. Any help would be appreciated.

Try:
update genrelinkmusicvideo t2
join musicvideo t1
on t2.idMVideo = t1.idMVideo
join genre t3
on t3.idGenre = t2.idGenre
set t2.idGenre = 62
where c11 <> strGenre and genre = 'Alternative Rock'
FYI, I would recommend using table_alias.column (e.g. t3.genre = 'Alternative Rock'), since it makes thing much more clear, particularly for those who do not know the schema already.

You could try:
update genrelinkmusicvideo
set idGenre=62
where idGenre IN (select t2.idGenre
from musicvideo t1
join genrelinkmusicvideo t2
on t2.idMVideo=t1.idMVideo
join genre t3
on t3.idGenre=t2.idGenre
where not (c11=strGenre) and genre='Alternative Rock')

Related

Update several select in MySQL

It's a sample to see the result.
Hello Everyone,
I wrote a query as a sample and I used several selects because I needed to add columns separately.
So, I got the result.
now I want to know if can I use an update statement for this query?
select T3.*,ao_00b950_aoproject_value.VALUE ODP from
(select T2.*,ao_00b950_aoproject_value.VALUE SOD from
(select T1.*,ao_00b950_aoproject_value.VALUE Platform from (SELECT project.LEAD,project.id,project.pname,project.pkey,ao_00b950_aoproject_value.VALUE Status FROM jira820db.project
LEFT JOIN jira820db.ao_00b950_aoproject_value
on project.ID=ao_00b950_aoproject_value.PROJECT_ID
and ao_00b950_aoproject_value.FIELD_ID =1) as T1
left join ao_00b950_aoproject_value
on T1.ID = ao_00b950_aoproject_value.project_ID
and ao_00b950_aoproject_value.field_ID=26) as T2
left join ao_00b950_aoproject_value
on T2.ID = ao_00b950_aoproject_value.project_ID
and ao_00b950_aoproject_value.field_ID=28) as T3
left join ao_00b950_aoproject_value
on T3.ID = ao_00b950_aoproject_value.project_ID
and ao_00b950_aoproject_value.field_ID=27
So, I wrote this update statement:
update
(select T3.*,ao_00b950_aoproject_value.VALUE ODP from
(select T2.*,ao_00b950_aoproject_value.VALUE SOD from
(select T1.*,ao_00b950_aoproject_value.VALUE Platform from (SELECT project.LEAD,project.id,project.pname,project.pkey,ao_00b950_aoproject_value.VALUE Status FROM jira820db.project
LEFT JOIN jira820db.ao_00b950_aoproject_value
on project.ID=ao_00b950_aoproject_value.PROJECT_ID
and ao_00b950_aoproject_value.FIELD_ID =1) as T1
left join ao_00b950_aoproject_value
on T1.ID = ao_00b950_aoproject_value.project_ID
and ao_00b950_aoproject_value.field_ID=26) as T2
left join ao_00b950_aoproject_value
on T2.ID = ao_00b950_aoproject_value.project_ID
and ao_00b950_aoproject_value.field_ID=28) as T3
left join ao_00b950_aoproject_value
on T3.ID = ao_00b950_aoproject_value.project_ID
and ao_00b950_aoproject_value.field_ID=27) as ww
set project.LEAD = 'kami'
where project.ID ='10000';
But unfortunately, it's not working and I'm not surprised because I'm not an expert in MySQL.
Does anybody have any suggestions?
Or I'm writing wrong the code like always.
With the first query, I get my result, and run perfectly.
With the second one, I get an error code 1288 "The target table ww of the UPDATE is not updatable".

How can I update the values on two tables with the values from another table with MySql

I have 3 tables, T1, T2, T3. I need to update the stock_status from T1 and limited from T2 with the values from stock_status and limited from T3, only where the sku are matching.
Also the entity_id is the correspondent for product_id.
Here is an image to understand better
I’m stuck at moving the values from stock_status from T3 in stock_status from T1, since I don’t have a common field directly.
For limited field, I tried.
UPDATE t2,t3 INNER JOIN t3 on t2.sku = t3.sku SET t2.limited = t3.limited
Try these
UPDATE t1
JOIN
t2
JOIN
t3
SET
t1.stock_status = t3.stock_status
WHERE
t1.product_id = t2.entity_id
AND t2.sku = t3.sku;
.
UPDATE t2
JOIN
t3
SET
t2.limited = t3.limited
WHERE
t2.sku = t3.sku;
You should use add an inner join between t2 and t1 for update also t1.stock_status
UPDATE t2,t1
INNER JOIN t3 on t2.sku = t3.sku
INNER JOIN t1 on t1.product_id = t2.entity_id
SET t2.limited = t3.limited,
t1.stock_status = t3.stock_status

Update query with multiple tables doesn't work

I'm trying to update a table in MySQL with data from another table.
UPDATE KassaticketRegels
SET soort = (SELECT t3.benaming
FROM KassaticketRegels AS t1 INNER JOIN Diensten AS t2 ON t1.dienst = t2.id INNER JOIN DienstGroepen AS t3 ON t2.dienstGroep = t3.id
WHERE t1.id = KassaticketRegels.id)
When i simulate the query, it gives me 304 matched rows.
But when i press go, i get the error "#1093 - Table 'KassaticketRegels' is specified twice, both as a target for 'UPDATE' and as a separate source for data".
How can i solve this?
Looking to your code seems you need an update on inner join
UPDATE KassaticketRegels t1
INNER JOIN Diensten AS t2 ON t1.dienst = t2.id
INNER JOIN DienstGroepen AS t3 ON t2.dienstGroep = t3.id
set t1.soort = t3.benaming

Whats wrong with MySQL query?

I am trying to delete rows from a table that spit out from a join:
DELETE FROM t1 WHERE company_name IN
(SELECT company_name FROM t1
LEFT OUTER JOIN t2
ON t2.company_name = t1.company_name
WHERE t2.name IS null)
Column 'company_name' in field list is ambiguous
Getting this ambiguous error while trying to make this query? Any suggestions?
MySQL doesn't like it when you try to UPDATE/DELETE a table and SELECT from the same table in the same query.
You can solve this with multi-table DELETE syntax:
DELETE t1 FROM t1 LEFT OUTER JOIN t2 USING (company_name)
WHERE t2.name IS NULL;
As the error message tells the column name company_name is not unique. Depending on your needs I believe this may solve your problem assuming you're trying to delete entries in t1 which doesn't have a corresponding row in t2 or have one but with name is null:
DELETE FROM
t1
WHERE
company_name NOT IN (
SELECT
t2.company_name
FROM
t2
WHERE
t2.name IS NOT NULL
)
Try that
DELETE FROM t1 WHERE company_name IN ( select * from (SELECT t1.company_name FROM t1 LEFT OUTER JOIN t2 ON t2.company_name = t1.company_name WHERE t2.name IS null) t )

UPDATE with JOIN and GROUP_CONCAT

I have 4 tables, one of which I need to update.
Table t1 needs to be updated according to the information in table t2 (t1.id = t2.id)
Table t2 contains information about websites (e.g.ID, traffic ).
Table t3 is a m:n table, that links the IDs in table t2 with the languages in table t4 based on language codes (ISO2) (e.g. XID: 1 | ISO2: EN,DE,FR)
Table t4 contains the ISO2-Codes (e.g. EN, DE, FR) and the respective languages (English, German, French)
Now I need to update the languages column in table t1 based on the information in tables t2,t3,t4.
I have written the following query, but it says SQL Error (1111): Invalid use of group function */
UPDATE t1
LEFT JOIN t2
ON t1.id = t2.id
LEFT JOIN t3
ON t2.id = t3.X_id
LEFT JOIN t4
ON t3.languages_iso2 = t4.iso2
SET t1.languages = GROUP_CONCAT(t4.`language` ORDER BY t4.language ASC)
I know that this solution can't be the most elegant one, but my SQL skills are not that good, so I don't know what else I should try. Does anyone have a solution for this problem?
Thanks in advance!
Try this:
UPDATE t1
INNER JOIN (SELECT t2.id, GROUP_CONCAT(t4.language ORDER BY t4.language) languages
FROM t2
INNER JOIN t3 ON t2.id = t3.X_id
INNER JOIN t4 ON t3.languages_iso2 = t4.iso2
GROUP BY t2.id
) AS t2 ON t1.id = t2.id
SET t1.languages = t2.languages;