How to query update in sql based on data from other table - mysql

I Have 2 Tables.
As Example:
table1
ID, data_static1, name1
1 8 Muna
2 1 Andi
3 7 null
table2
ID, data_static2, name2
1 0 Aji
2 1 Andi
3 2 max
4 3 nadine
5 4 Rio
6 5 Panji
7 6 Eko
8 7 Pan
9 8 Muna
I want to update the column name1 in table1 based on the largest ID in table1 where table1.data_static1 is the same as table2.data_static2.
I want the results as below
table1
ID, data_static1, name
1 8 Muna
2 1 Andi
3 7 Pan
I've tried the following code
mysql> UPDATE theDB.table1 SET name1=(SELECT name2 FROM table2 WHERE data_static2=(SELECT data_static1 From table1 WHERE ID IN(SELECT MAX(ID) FROM table1))) WHERE table1.ID IN(SELECT MAX(table1.ID) FROM theDB.table1);
I get an error message
ERROR 1093 (HY000): You can't specify target table 'table1' for update in FROM clause

Simplest solution would be to use a correlated subquery for this:
update table1 t1
set name1 = (
select name2
from table2 t2
where t1.data_static1 = t2.data_static2
order by id desc limit 1
);
You can use JOIN too:
update table1 t1
join (
select *
from table2 t
join (
select data_static2,
max(id) as id
from table2
group by data_static2
) t2 using (data_static2, id)
) t2 on t1.data_static1 = t2.data_static2
set t1.name1 = t2.name2;

Related

Left join with other table with where clause

I have 2 tables in MySQL database that I would like to join, where I would like to contain all results from table1.
My tables looks like this:
table1
id
name
1
name1
2
name2
3
name3
4
name4
5
name5
6
name6
7
name7
8
name8
table2
id
table1_id
myfield
1
3
test1
2
2
test2
3
1
test1
4
4
test2
5
5
null
6
2
null
What I am trying to achieve is to get a table which contains all the rows from table1 and only the data that is joined from table2.
This is my query:
select * from table1 as t1
left join table2 as t2 on t1.id = t2.table1_id
where myfield="test1"
group by t1.id
But this is not what I want to achieve.
What I want to achieve is to get all records from table1 and to have all related records from table2 where table2.myfield="test1". And for the other for table2.mytable to have null (if they do not fulfil table2.myfield="test1").
Any help is much appreciated!
Thanks!
move the where clause to the on clause:
select * from table1 as t1
left join table2 as t2 on t1.id = t2.table1_id
and myfield="test1"
group by t1.id
BTW: some DBMS does not allow select * with group by. So select the id and some aggregated values or remove group by id

Select query except another table

I got 2 tables.
TABLE 1
ID FRANCHISENAME TELEPHONE FRANCHISE_ID
1 BURGER 666-555-999 5
2 JSUBS 666-555-999 7
3 STEAKS 777-888-999 3
TABLE 2
ID NAME TELEPHONE EMAIL FRANCHISE_ ID
5 JOHN 555-444-333 JOHN#GMAIL.COM 5
5 JOHN 555-444-333 JOHN#GMAIL.COM 7
6 EDGARD 555-444-333 EDGARD#GMAIL.COM 9
I want to retrieve all data in table one, except for that data where the user has his email in Table 2. As for example JOHN has franchise_id 5 and 7, so the query would only return
3 STEAKS, 777-888-999, 3
Assuming that TABLE_1 & TABLE_2 relate to each other through TABLE_1.FRANCHISE_ID & TABLE_2.FRANCHISE_ID
You can use NOT EXISTS
SELECT
*
FROM TABLE_1 T1
WHERE NOT EXISTS(
SELECT *
FROM TABLE_2 T2
WHERE T2.FRANCHISE_ID = T1.FRANCHISE_ID
AND T2.EMAIL = 'JOHN#GMAIL.COM'
)
OR
You can use LEFT JOIN along with IS NULL
SELECT
T1.*
FROM TABLE_1 T1
LEFT JOIN TABLE_2 T2 ON T1.FRANCHISE_ID = T2.FRANCHISE_ID
WHERE T2.FRANCHISE_ID IS NULL;
SELECT t1.*
FROM
Table1 t1
LEFT JOIN Table2 t2
ON t1.FRANCHISE_ID = t2.FRANCHISE_ID
AND LEN(IFNULL(t2.EMAIL,'')) > 0
WHERE
t2.ID IS NULL
Even if there is a record in Table 2 if it has no email it will be returned by this query. You can expand say to > 7 or more to check for a minimum level of validity of email address.
This should get you there using the NOT IN function. It will exclude records from Table 1 if there is a matching Franchise ID in Table 2, unless the email field is Table 2 is null:
SELECT * FROM Table1
WHERE Table1.Franchise_ID NOT IN
(SELECT Table2.Franchise_ID FROM Table2
WHERE Table2.Email IS NOT NULL);

Finding all children with duplicate columns

I am trying to find duplicates in a table column only if the row is also a child of another table ex:
table 1 columns
id
Type
table 2 columns
id
table1Id
table3Id
Sample Data:
table 1:
id Type
1 aType
2 myType
3 myType
4 myType
5 myType
6 myType
table 2:
id table1Id table3Id
1 1 1
2 2 1
3 4 2
4 5 1
5 6 2
Results I'd like: (rows in table1 with same Type and table3Id)
table1Id table1Type table3Id
2 myType 1
5 myType 1
4 myType 2
6 myType 2
query I tried:
select t1.id as table1Id, t1.type as table1Type, t2.table3Id
from table1 t1 inner join
table2 t2
on t1.id = t2.table1Id inner join
table1 a
on t1.Type = a.Type and a.id <> t1.id
where t1.Type = 'myType' ;
The above query gives me hundreds of repeats of the same row, with around 500,000 rows returned.
You seem to want pairs of table3id and table1type that are the same. Here is a method that returns results in a slightly different format:
select t2.table3id, t1.type, group_concat(t1.id) as table1ids
from table1 t1 join
table2 t2
on t1.id = t2.table1id
group by t2.table3id, t1.type
having count(*) > 1;
This puts the ids with the same value in a list.
This works fine for me:
drop table if exists table1;
drop table if exists table2;
create table test_delete.table1 as
select 1 id , 'aType' typ union all
select 2 ,'myType' union all
select 3 ,'myType' union all
select 4 ,'myType' union all
select 5 ,'myType' union all
select 6 ,'myType';
create table test_delete.table2 as
select 1 id , 1 table1Id , 1 table3Id union all
select 2 ,2 , 1 union all
select 3 ,4 , 2 union all
select 4 ,5 , 1 union all
select 5 ,6 , 2;
select * from table1;
select
t1.id, t1.typ, t2.table3Id
from
table1 t1
inner join
table2 t2 on t1.id = t2.table1Id
where
t1.typ = 'myType'
order by t2.table3Id;

Mixing 3 sql queries in 1

I have 3 tables as follows
Table1
Id Name
1 abcd
2 bcd
3 dabc
Table2
Id2 Name2
2 xyz
3 def
4 mno
Table3
Id Id2 Value
1 4 1
2 3 1
3 4 1
Now,
From table1 : I have to select all Id where Name is %abc%
From table2: I have to select Id2 where Name2 is "mno"
From Table3: I have to change value to 0 from 1 where Id's value are from Table1 and Id2 is from Table2.
Table 1:
select Id from Table1 where Name like '%abc%'
Table2 :
select Id2 from Table2 where Name2 = "mno"
Table 3:
update Table3 set Value = 0 where Id in() and Id2=
But, I dont know how to make it 1 single query. Can anyone please guide me up ?
Refer to: prior stack article
You've not explained how T1 relates to T2, So I have assumed a cross join.
Whenever you have a record in T1 with name like '%abc%' (1,3) in your data..
and whenever you have a record in T2 with a name equal to 'mno' 4 then you want the value in table 3 to be 0
so the select we generate should produce
1,4
3,4
and when we inner join this back to table 3 it only selects
Id Id2 Value
1 4 1
3 4 1
Now we generate an update based on this select as outlined in the link provided above...
UPDATE table3
INNER JOIN (
SSELECT t1.ID t1ID, t2.id t2ID
FROM table1 t1
CROSS JOIN table2
WHERE t1.name like '%abc%'
and t2.name like = 'mno') B
on B.t1ID = t3.Id
and B.t2ID = T3.ID2
SET value = 0
Giving us a result of
Id Id2 Value
1 4 0
2 3 1
3 4 0
if we select * from table3
update t3
set t3.Value = 0
from Table3 t3
inner join Table1 t1
on t3.Id = t1.Id
inner join Table2 t2
on t3.Id2 = t2.Id2
where t1.Name like '%abc%' and t2.Name2 = 'mno'
OR
update Table3
set value = 0
where Id in (select Id from Table1 where Name like '%abc%')
and Id2 in (select Id2 from Table2 where Name2 = 'mno')
You should think about UPDATE ... WHERE EXISTS as follows:
update Table3 set Value = 0
WHERE EXISTS (SELECT 1 FROM Table1 where Name LIKE '%abc%' AND Table1.Id=Table3.Id )
AND EXISTS (SELECT 1 FROM Table2 where Name2 = "mno" AND Table2.Id2=Table3.Id2)

How to select a column from one table and two columns from another table when both the tables have no relation in T-SQL?

I will explain my problem as simple as possible.
I have written a select query Query1 on Table1 which gives the me the following result
SELECT * FROM Table1 WHERE TypeID=1
ID Column1 Column2 TypeID
1 A A 1
2 B B 1
3 C C 1
I have another table Table2 which has data in the following format
ID Column1 Column2
1 0 0
1 1 1
2 2 2
2 3 3
2 4 4
3 5 5
3 6 6
I have written a another select query Query2 on Table1 which gives the me the following result
SELECT * FROM Table1 WHERE TypeID=2
ID Column1 Column2 TypeID
4 A A 2
5 B B 2
6 C C 2
Table1 and Table2 have no column in common and data in Column1 and Column2 for both TypeID's in Table1 is same and currently data in Table2 has data with values of ID column from Table1 for id's 1,2,3 only and i want to write a select query to select same data from Table2 but with values of ID column from Table1 with TypeID as 2 which I have given below
ID Column1 Column2
4 0 0
4 1 1
5 2 2
5 3 3
5 4 4
6 5 5
6 6 6
how can I achieve this by writing a select query in sql server?
select T1.column1, isnull(T2.column2,0) as column2,
isnull(T2.column3,0) as column3
from table1 T1
left outer join on Table2 T2 on T1.column9=T2.column9
where ....
If I understood the question correctly, the query whould be like that:
SELECT
T_ID.T2_ID,
Column1,
Column2
FROM
Table2
JOIN
(
SELECT
T1.ID AS T1_ID,
T2.ID AS T2_ID
FROM
Table1 AS T1
JOIN Table1 AS T2 ON T1.Column1 = T2.Column1 AND T1.Column2 = T2.Column2
WHERE
T1.TypeID = 1
AND T2.TypeID = 2
) AS T_ID ON Table2.ID = T_ID.T1_ID