create view joining two tables having same columns without duplicates - mysql

Can anyone please help me on how to create view joining two tables having same columns without duplicates?
Example:
I have two tables T1 and T2
T1
Id Name Date
-----------------------
1 AAA 2019-04-05
2 BBB 2019-04-06
3 CCC 2019-04-07
T2
Id Name Date
----------------------
4 DDD 2019-04-01
1 ABC 2019-03-01
2 DEF 2019-03-02
My output view should look like this
Id Name Date
------------------------
1 AAA 2019-04-05 (From T1)
2 BBB 2019-04-06 (From T1)
3 CCC 2019-04-07 (From T1)
4 DDD 2019-04-01 (From T2)
Below is the query I am trying
CREATE VIEW view AS (
(
SELECT
t1.id,
t1.name,
t1.date,
FROM
T1 as t1
UNION
SELECT
t2.id,
t2.name,
t2.date,
FROM
T2 as t2
)
But I get duplicate records.

You seem to want a prioritization query, with table1 prioritized over table2. In MySQL:
select t1.id, t1.name, t1.date
from table1 t1
union all
select t2.id, t2.name, t2.date
from table2 t2
where not exists (select 1 from table1 t1 where t2.id = t1.id);
This selects all rows from table1 and then selects all rows from table2 that don't have a matching id in table1.
This assumes that id is sufficient for determining which records are "duplicates".

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

Correlated subquery & conditional where clause in Sequelize

I have a correlated subquery that is validated based on the column value. Even though I read through the sequelize docs and examples on GitHub, I did not really get any working solutions.
Query I am trying to execute
select count(*) from table1 t1
where case when deciding_col=1 then t1.id in
(select distinct t2.id from table2 t2 where t2.id=t1.id and t2.second_condition=132)
else 1=1 end;
Any references or suggestions are appreciated.
Update:
The goal is to fetch all records from table1, but if deciding_col value is true for a record, then I need to check if that particular record exist in table2.
Example:
Table1 Data:
id | name | deciding_col
1 abcd 1
2 edfg 0
3 xyz 0
Table2 Data:
id | second_condition
1 132
Output:
1.
select t1.* from table1 t1
where case when deciding_col=1 then t1.id in
(select distinct t2.id from table2 t2 where t2.id=t1.id and t2.second_condition=132)
else 1=1 end;
id | name | deciding_col
1 abcd 1
2 edfg 0
3 xyz 0
2.
select t1.* from table1 t1
where case when deciding_col=1 then t1.id in
(select distinct t2.id from table2 t2 where t2.id=t1.id and t2.second_condition=133)
else 1=1 end;
id | name | deciding_col
2 edfg 0
3 xyz 0

SQL Combining data from 3 tables to from a 4th table based on some conditions

I have 3 tables:-
table1 :-
ReportType | ResourceId
t2 123
t3 5
table2:-
Id | Name | Created
1 A 10
2 B 11
123 C 12
table3:-
Id | Name | Created
4 D 13
5 E 14
6 F 15
table1's ResourceId and table2 and 3's Id column have same values
I want to create a 4th table like this:-
ReportType | ResourceId | Name | Created
t2 123 C 12
t3 5 E 14
such that wherever table1's ReportType is t2 I want the Name and Created value from table2 for the condition table1.ResourceId = table2.Id and wherever table1's ResourceType is t3 I want the Name and Created value from table3 for the condition table1.ResourceId = table3.Id.
PS: This isn't some sort of HomeWork. I have been stuck at this query for the past 1 hour, I have read various answers and tried a few queries of my own before posting the question. Any help would really be appreciated.
Explanation in comments :)
--here we join first and second table, but we filter results to include only ReportType = t2
select t1.ReportType, t1.ResourceId, t2.Name, t2.Created from table1 as t1 join table2 as t2 on t1.ResourceId = t2.id
where t1.ReportType = 't2'
union all
--here we join first and third table, but we filter results to include only ReportType = t3
select t1.ReportType, t1.ResourceId, t3.Name, t3.Created from table1 as t1 join table3 as t3 on t1.ResourceId = t3.id
where t1.ReportType = 't3'
You can use the below query:
select report_type, resourceid,name, created from dbo.t2, dbo.t1
where report_type='t2' and ResourceId=id
UNION
select report_type, resourceid,name, created from dbo.t3, dbo.t1
where report_type='t3' and ResourceId=id;

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

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;