How to update SQL on JOIN - mysql

So I have two tables. T1 is completely filled and looks like this:
ean | id | title | price |
T2 looks the same, but has missing records. It contains the id's of T1. So what I wanna do, is fill in the empty records with T1's data on T1.id = T2.id.
I looked up my question on stackoverflow and came up with this code:
UPDATE monitoren
SET T2.ean = T1.ean
FROM T2
JOIN T1
ON T1.id = T2.id;
(Of course I also wanna update the columns title and price in T2.)
This gives me a syntax error at 'FROM T1 JOIN T2 ON T1.id = T2.id'

MySQL does not support a FROM clause. Instead, you express the join like this:
UPDATE T2 JOIN
T1
ON T1.id = T2.id
SET T2.ean = T1.ean ;

Related

How to find latest row in a table corresponding to each row in a different table?

I thought I will get answer for this query straight away but I didn't find any Q&A which was exactly what I am looking for.
So, I have two tables.
Table 1: id,c_id,created_at
Table 2:id,c_id,b_date,s_type
I want a query which gives output corresponds to each row in table 1. The output should be the latest b_date in table 2 prior to created_at in Table 1 and the s_type corresponding to the latest b_date. The linking condition between Table 1 and Table 2 isc_id. Both the tables have multiple rows containing same c_id.
My table 1,table 2 and output will look like this.
I am not allowed to embed images as of now. So please click to see the images.
Possibly this
SELECT T1.ID,T1.C_ID,T1.CREATED_AT,T2.B_DATE,T2.S_TYPE
FROM TABLE1 T1
JOIN TABLE2 T2 ON T1.C_ID = T2.C_ID
WHERE T2.B_DATE = (SELECT MAX(T3.B_DATE) FROM TABLE2 T3 WHERE T3.b_DATE < T1.CREATED_AT AND T3.C_ID = T1.C_ID)
;
or maybe this
SELECT S.ID,S.C_ID,S.CREATED_AT,S.B_DATE,T.S_TYPE
FROM
(
SELECT T1.ID,T1.C_ID,T1.CREATED_AT,T2.B_DATE,T2.S_TYPE
FROM TABLE1 T1
JOIN TABLE2 T2 ON T1.C_ID = T2.C_ID
WHERE T2.B_DATE = (SELECT MAX(T3.B_DATE) FROM TABLE2 T3 WHERE T3.b_DATE < T1.CREATED_AT AND T3.C_ID = T1.C_ID)
) S
JOIN
(SELECT T1.ID,T1.C_ID,T1.CREATED_AT,T2.B_DATE,T2.S_TYPE
FROM TABLE1 T1
JOIN TABLE2 T2 ON T1.C_ID = T2.C_ID
WHERE T2.B_DATE = (SELECT MAX(T3.B_DATE) FROM TABLE2 T3 WHERE T3.C_ID = T1.C_ID)
) T ON S.C_ID = T.C_ID
;

How to construct sql statement to select only if the content is available in JOIN from two tables

I have two tables as such.
t1
id name
1 foo
2 bar
t2
tid id address
1 1 foss
I can execute select satement to select id 1 from t1 from both tables.
select * from t1 inner join t2 on (t1.id = 1 and t2.id = 1)
But, what I actually want is select bar from t1, and only bar, even though it isn't present in t2.
The above tables are similar to what I have been working on my project, and resembles to the same problem.
Here is what I tried.
1. I used left join t1.id = 2 and t2.id = 2
It lists all the values from t1, which I only wanted t1.id = 2.
2.inner join could be used, but since t2.id = 2 is not present.
Therefore, it doesn’t return anything.
But I wanted something that would display data from t1 and t2 if id is present in both tables, and if not display only from table t1 of only that id.
So, if I wanted to display t1.id = 2, the answer I would expect is using
select * from t1 join .....
id name tid id address
2 bar null null null
Based on your expected result this is a simple Left Join:
select *
from t1 left join t2
on t1.id = t2.id
where t1.id = 2
Use left join to get all elements of t1 even if a element of t2 with same id not exists.
Then use null to get only t1 when t2 not exists.
At last, you can add a where filter to get a subset of t1 (ie: id = 2)
SELECT t1.*, t2.*
FROM t1
LEFT JOIN t2
ON t1.id = t2.id
WHERE t2.id IS NULL
AND t1.id = 2;

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;

MySQL UPDATE SET field equals to SELECT

I'm trying to update several rows with email addresses. T1.email is to be updated with T2.email based on T1.name existing in T2.name.
Currently, the query looks like this.
UPDATE T1
SET T1.email = T2.email
FROM Table1 T1
INNER JOIN Table2 T2 ON T1.name = T2.name
WHERE T1.name = T2.name
AND (Some conditions)
LIMIT 1398
A similiar question is asked here, but a syntax error is given.
SQL UPDATE SET one column to be equal to a value in a related table referenced by a different column?
I've also tried updating with ANY.
UPDATE Table1
SET Table1.email = ANY
(
SELECT Table2.email FROM Table2
WHERE Table1.accountid = 901234
AND Table2.pid = 123
AND Table2.email IS NOT NULL
)
WHERE Table1.name IN
(
SELECT Table2.name FROM Table2
WHERE Table1.accountid = 19574
AND Table2.pid = 123
AND Table2.email IS NOT NULL
)
LIMIT 1398
Currently, this returns an error "SQL Error (1242): Subquery returns more than 1 row".
May be the beginning of a copy and paste job!
As detailed under UPDATE Syntax, for MySQL you want:
UPDATE Table1 T1 JOIN Table2 T2 USING (name)
SET T1.email = T2.email
WHERE (Some conditions)
LIMIT 1398