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

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

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

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

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;

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)

Sort output from multiple tables

Query1(by combining table1, table 2): returns
SELECT t1.ID1,t1.Name
FROM table1 t1, table2 t2
WHERE t1.ID1=t2.ID;
ID1 Name
4 ppp
1 pqr
2 abc
3 xyz
Query2(using table 3, which stores IDs): returns
select ID from table3;
ID
1
2
3
4
Combine Query1 & Query 2 & produce output as
ID Name
1 pqr
2 abc
3 xyz
4 ppp
ie main values are coming from 2 different values while sorted values(IDs) are stored in random order in third table.
something like:-
select t1.ID1,t1.Name from table1 t1, table2 t2
LEFT JOIN table3 t3 ON t3.ID = t1.ID where t1.ID=t2.ID;