Sort output from multiple tables - mysql

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;

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

create view joining two tables having same columns without duplicates

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".

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)

mysql to select records where values are in sequence

How can I select records where ID1 values are in sequence (of two or more) where TOWN matches
My table
TOWN ID1
town1 1
town1 2
town1 4
town2 1
town2 5
town2 8
town3 1
town3 2
town3 3
required result
TOWN ID1
town1 1
town1 2
town3 1
town3 2
town3 3
sql fiddle
http://sqlfiddle.com/#!2/b409f/26
You can use an EXISTS clause to check for the next value in the sequence. This code will only match "sequences" of length >= 2, which seems to be what you want from your example.
SELECT *
FROM Table1 a
WHERE EXISTS (SELECT *
FROM Table1 b
WHERE b.TOWN=a.TOWN
AND b.ID1 IN (a.ID1 - 1, a.ID1 + 1))
ORDER BY TOWN, ID1
If you question is "give me all rows that have an adjacent id1 field for the town", then simply:
select distinct t1.*
from Table1 t1
join Table1 t2 on t2.town = t1.town and ABS(t1.ID1 - t2.ID1) = 1
order by 1, 2
See SQLFiddle for this.
To also match on another column, add the condition to the join, eg:
select distinct t1.*
from Table1 t1
join Table1 t2
on t2.town = t1.town
and t2.state = t1.state
and ABS(t1.ID1 - t2.ID1) = 1
order by 1, 2

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