Update query with multiple tables doesn't work - mysql

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

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

MySQL: join with an unused table increases execution time?

Assuming t is a large table, and the following two queries
SELECT t1.value, t2.value
FROM t as t1 JOIN t as t2
ON t1.id = t2.id
WHERE t1.key = '123'
and
SELECT t1.value, t2.value
FROM t as t1 JOIN t as t2 JOIN t as t3
ON t1.id = t2.id
WHERE t1.key = '123'
the second one having a JOIN with a table that is not used in the SELECT.
The second query executes much slower. I expected that MySQL would figure out that the third JOIN is not used and will just ignore it. But it does not?
Your second query doesn't have an ON clause for the second join:
SELECT t1.value, t2.value
FROM t as t1
JOIN t as t2
JOIN t as t3 ON t1.id = t2.id
WHERE t1.key = '123';
This means that every matching record in t1 will be joined onto every record in t2. This is, perhaps, what you meant:
SELECT t1.value, t2.value
FROM t as t1
JOIN t as t2 ON t1.id = t2.id
JOIN t as t3 ON t1.id = t3.id
WHERE t1.key = '123';
This will perform much more reasonably because it isn't creating a huge number of results.
If you intended to do a full join onto t3:
SELECT t1.value, t2.value
FROM t as t1
JOIN t as t2 ON t1.id = t2.id
JOIN t as t3
WHERE t1.key = '123';
Then this will be slower because, even though you are not SELECTing a field from t3 it does change the output because it produces extra rows.
See here for examples http://sqlfiddle.com/#!9/e86c9/3
This is because the default JOIN in mySQL implies INNER JOIN.
The third join will not be ignored because this will alter the eventual data set you get back after executing the query.
This stackoverflow question contains more detailed information
It is not that the MySQL optimizer isn't smart enough to remove the unused query, it is just that you are using the wrong syntax here. As the documentation states, your query will be performed as:
JOIN t as t2 JOIN t as t3 --> t2 CROSS JOIN t3
The syntax you are using isn't standard SQL and cannot be used in any SQL standard compliant database. Take a look at the specific MySQL JOIN documentation here .

SQL query to have data in field instead of new record

I have searched for this but I'm probably using the wrong terminology.
The following query
SELECT t1.name, t2.entry FROM t1
INNER JOIN t2 ON t2.ID = t1.ID
WHERE t2.meta_key IN ('wp_x1','wp_x2');
Returns data similar to below where there are 2 records for each of the meta_key fields
name1,wp_x1_entry
name1,wp_x2_entry
name2,wp_x1_entry
name2,wp_x2_entry
How do I amend the query to return this instead?
name1,wp_x1_entry,wp_x2_entry
name2,wp_x1_entry,wp_x2_entry
The table/field names have been changed to hide sensitive info. Also, I know these are badly designed tables but I am unable to change the db structure.
This will be calling a mySql db from C# code.
Join another time with t2, looking for wp_x2 only
SELECT t1.name, t2.entry, t3.entry
FROM t1
JOIN t2 ON t2.ID = t1.ID and t2.meta_key = 'wp_x1'
JOIN t2 as t3 ON t3.ID = t1.ID and t3.meta_key = 'wp_x2'
Will return only rows with both wp_x1 and wp_x2. Use LEFT_JOIN if one/none is required.
Try to Group By t1.name and group_concat t2.entry.
like this :
SELECT t1.name, GROUP_CONCAT(t2.entry) FROM t1
INNER JOIN t2 ON t2.ID = t1.ID
WHERE t2.meta_key IN ('wp_x1','wp_x2')
GROUP BY t1.name;
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html

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;