I have 3 tables. 2 of them are the same (same columns, different data), and the third has some info data about other 2. Database looks like this:
Table 1:
+--------------+
| ID | Name |
+--------------+
| 1 | Table 2 |
| 2 | Table 3 |
+--------------+
Table 2:
+-------------------------------+
| Name | Temperature | Pressure |
+-------------------------------+
| Table 2 | 22 | 1013 |
+-------------------------------+
Table 3:
+-------------------------------+
| Name | Temperature | Pressure |
+-------------------------------+
| Table 3 | 20 | 1009 |
+-------------------------------+
I'm trying to JOIN all into one table, which should look like this:
+-------------------------------+
| Name | Temperature | Pressure |
+-------------------------------+
| Table 2 | 22 | 1013 |
| Table 3 | 20 | 1009 |
+-------------------------------+
Any idea how sql query should look like?
Thanks
Try union:
SELECT table1.name, temperature, pressure
FROM table1 inner join table2 ON
table1.name = table2.name
UNION
SELECT table1.name, temperature, pressure
FROM table1 inner join table3 ON
table1.name = table3.name
Edit:
You can make another select from those results, then you can limit, group or order:
SELECT * FROM
(
SELECT table1.name, temperature, pressure
FROM table1 inner join table2 ON
table1.name = table2.name
UNION
SELECT table1.name, temperature, pressure
FROM table1 inner join table3 ON
table1.name = table3.name
) as JoinedTable
LIMIT 0, 1
Edit 2:
To have only one row from each table (table 2 and table 3) you can use limit/group by/order by for each query (assuming you have column date):
SELECT table1.name, temperature, pressure
FROM table1 inner join table2 ON
table1.name = table2.name
ORDER BY date DESC
LIMIT 0, 1
UNION
SELECT table1.name, temperature, pressure
FROM table1 inner join table3 ON
table1.name = table3.name
ORDER BY date DESC
LIMIT 0, 1
You can use the UNION statement:
(
SELECT Name, Temperature, Pressure
FROM Table1 INNER JOIN Table2 ON Table1.Name = Table2.Name
)
UNION
(
SELECT Name, Temperature, Pressure
FROM Table1 INNER JOIN Table3 ON Table1.Name = Table3.Name
)
Related
I need help in joining two tables with a specific condition. I want to left join Table_1 with Table_2 on the nearest Table_A.Col_A <= Table.Col_B
So I have two tables
Table_1
Col_A
1
2
6
Table_2
Col_A | Col_B
1 | p1
4 | p2
5 | p3
Result Table
Col_A | Col_B
1 | p1
2 | p1
6 | p3
A correlated subquery handles this:
select t1.col_a,
(select t2.col_b
from table2 t2
where t2.col_A <= t1.col_A
order by t2.col_A desc
limit 1
)
from table1 t1;
I would try doing something like this.
SELECT DISTINCT t1.col1, t2.col2
FROM Table_1 t1 JOIN Table_2 t2 on t1.col1 <= t2.col1
WHERE --something matches
However there is not much information on this post, the more details you can provide should allow us to help you better.
You can put the condition in the ON clause like this:
select
t1.Col_A, t2.Col_B
from Table_1 t1 left join Table_2 t2
on t2.Col_A = (select max(Col_A) from Table_2 where Col_A <= t1.Col_A)
See the demo.
Results:
| Col_A | Col_B |
| ----- | ----- |
| 1 | p1 |
| 2 | p1 |
| 6 | p3 |
I have two similar tables
Table 1
| id | name | amount|
| 2 | Mike | 1000 |
| 3 | Dave | 2500 |
Table 2
| id | name | amount|
| 2 | Mike | 1200 |
| 4 | James| 2500 |
I want to query the tables to get a result like this:
| id | name | amount_table1| amount_table2|
| 2 | Mike | 1000 | 1200 |
| 3 | Dave | 2500 | |
| 4 | james| | 2500 |
UNION ALL the tables. Do GROUP BY to get one row per id/name combo.
select id, name, sum(amount1), sum(amount2)
from
(
select id, name, amount as amount1, null as amount2 from table1
union all
select id, name, null, amount from table2
) dt
group by id, name
You need to do union with left and right join
select a.id , a.name , a.amount amount_table1,b.amount amount_table2 from table1 a left join table2 b on (a.id=b.id)
union
select b.id , b.name ,a.amount,b.amount from table1 a right join table2 b on (a.id=b.id)
MySql doesn't support FULL OUTER JOIN.
But it supports LEFT & RIGHT joins and UNION.
select
t1.id, t1.name, t1.amount as amount_table1, t2.amount as amount_table2
from Table1 t1
left join Table2 t2 on t1.id = t2.id
union all
select t2.id, t2.name, t1.amount, t2.amount
from Table2 t2
left join Table1 t1 on t2.id = t1.id
where t1.id is null
The first select will get those only in Table1 and those in both.
The second select will get those only in Table2.
And the UNION glues those resultsets together.
If this were for a database that supports FULL JOIN then it would be simplified to:
select
coalesce(t1.id, t2.id) as id,
coalesce(t1.name, t2.name) as name,
t1.amount as amount_table1,
t2.amount as amount_table2
from Table1 t1
full join Table2 t2 on t1.id = t2.id
I have two tables.
table1:
| ID | NAME |
|----|------|
| 1 | aaa |
| 2 | aaa |
| 3 | aaa |
| 4 | bbb |
| 5 | bbb |
table2:
| ID | DATE |
|----|----------|
| 1 | 12/07/10 |
| 2 | 12/07/13 |
| 3 | 12/07/16 |
| 4 | 12/07/08 |
| 5 | 12/07/20 |
Help me pls, I don't know how to SELECT MAX ID in table1 by date in table2.
For example result should be:
for "aaa": ID 3 from table2
for "bbb": ID 5 from table2
I'm trying something like that:
DATE = (SELECT MAX(DATE) FROM table2 t2, table1 t1 WHERE t1.NAME = "aaa")
But it's not working... Have you got some idea?
You need to add an additional condition (t1.id = t2.id) for the join:
SELECT MAX(DATE)
FROM table2 t2, table1 t1
WHERE t1.NAME = "aaa"
AND t1.id = t2.id
But please - don't join with comma - use explicit JOIN syntax instead:
SELECT MAX(DATE)
FROM table2 t2
JOIN table1 t1
ON t1.id = t2.id
WHERE t1.NAME = "aaa"
You can also get all max dates for all names at once using GROUP BY name:
SELECT t1.NAME, MAX(t2.DATE)
FROM table2 t2
JOIN table1 t1
ON t1.id = t2.id
GROUP BY t1.NAME
I have tried to take the MAX Date using this query.
Note: In the table the date format should be (Y-m-d) and the field should be set to date in order to manipulate the operations
Table - 1(Name: test1)
Table - 2(Name: test2)
QUERY TO GET MAX FROM OTHER TABLE
SELECT MAX(dates) FROM test2 JOIN test1 WHERE test1.name='aaa'
OUTPUT
Hope so this query will solve your problem.
select ID from Table1 where date= (select max(date) from T2 where name= 'aaa'
i didn't fully got you. But I think this is how you want it.... If you had these two table as one table then it would work as charm.
Or THIS ,with Two Table like you asked for
SELECT T1.Id FROM T1 INNER JOIN T2
ON T1.Id = T2.Id
where T2.date = (select max(T2.date) from T1 where T1.name= 'aaa')
I've a huge problem, I don't what is going on. This is my
table1
+-----+-----------------------+--------------+
| id | invoice_number | invoice_date |
+-----+-----------------------+--------------+
| 12 | 12536801244 | 2009-09-23 |
| 38 | 12585302890 | 2009-11-18 |
| 37 | 12584309829 | 2009-11-16 |
| 123 | 12627605146 | 2010-01-06 |
| 191 | 12663105176 | 2010-02-16 |
+-----+-----------------------+--------------+
and this is my second
table2
+-----+-----------------------+--------------+
| id | invoice_number | invoice_date |
+-----+-----------------------+--------------+
| 12 | 1t657801244 | 2009-09-23 |
| 20 | 12585302890 | 2009-11-18 |
| 37 | 1ss58430982 | 2009-11-16 |
| 103 | 12627605146 | 2010-01-06 |
| 121 | 12346310517 | 2010-02-16 |
+-----+-----------------------+--------------+
What I want is, I have get all invoice_numbers that are not in table2
This is my SQL query.
select t2.invoice_number FROM table1 t1
JOIN table2 t2 ON t2.invoice_number != t1.inovice_number;
But I get different result. Any body what is wrong with SQL code?
Performance wise you can use Left Join or Not EXIST
Try with Left Join
Select t2.invoice_number
FROM table1 t1
LEFT JOIN table2 t2 ON
t2.invoice_number = t1.inovice_number AND
t2.invoice_number IS NULL;
Try With Not Exist
Select t2.invoice_number
FROM table1 t1 Where Not Exist
(
SELECT NULL
FROM table2 t2
WHERE t2.invoice_number = t1.inovice_number
)
Try with Not IN
Select t2.invoice_number
FROM table1 t1
Where t1.inovice_number NOT IN
(
SELECT t2.inovice_number
FROM table2 t2
)
Why don't you simply try:
Select * from Table1
Where Table1.invoice_number NOT IN (select invoice_number from Table2)
You can use LEFT JOIN.
SELECT t1.invoice_number
FROM table1 t1
LEFT JOIN table2 t2 ON t2.invoice_number = t1.inovice_number
WHERE t2.invoice_number IS NULL
Why not use where not exists?
SELECT *
FROM table1 t1
WHERE NOT EXISTS (SELECT * FROM table2 t2 WHERE t1.invoice_number = t2.invoice_number)
That's not the correct way to query what you are looking for.
You probably need something like this:
select invoice_number from table2
where invoice_number not in (select invoice_number from table1)
select * from table1 where invoice_number NOT EXISTS (select invoice_number FROM table2 WHERE table1.invoice_number = table2.invoice_number);
select t2.id FROM table1 as t1
INNER JOIN table2 as t2 ON t2.id != t1.id;
I have two tables that I would like to display as a single result, using UNION or other technique.
The ID field relates both tables.
The second table has one field missing, so that missing value should be assumed from the first table.
The sample code below works, but is very slow for large datasets.
Is there any solution more efficient?
T1: T2:
+----+-------+--------+ +----+------+
| id | name | town | | id | name |
+----+-------+--------+ +----+------+
| 1 | Alice | London | | 1 | Bob |
| 2 | Alan | Zurich | +----+------+
+----+-------+--------+
Desired result:
+----+-------+--------+
| id | name | town |
+----+-------+--------+
| 1 | Alice | London |
| 2 | Alan | Zurich |
| 1 | Bob | London |
+----+-------+--------+
Sample code:
with T1 as
(
select * from
(
values
(1,'Alice','London') ,
(2,'Alan','Zurich')
) as t (id,name,town)
), T2 as
(
select * from
(
values
(1,'Bob')
) as t (id,name)
), T2WithTown as
(
select t2.id,t2.name,t1.town from T2
inner join T1 on t2.id=t1.id
)
select id,name,town from T1
union
select id,name,town from T2WithTown
The sample data shows both tables have distinct values. So I will prefer UNION ALL
Select ID, Name, Town From T1
UNION ALL
select T2.ID, T2.Name, T1.Town
from T1
INNER JOIN T2 on T2.id = T1.id
Just like this:
with T1 as
(
select * from
(
values
(1,'Alice','London') ,
(2,'Alan','Zurich')
) as t (id,name,town)
), T2 as
(
select * from
(
values
(1,'Bob')
) as t (id,name)
), T2WithTown as
(
select t2.id,t2.name,t1.town from T2
inner join T1 on t2.id=t1.id
)
select id,name,town from T1
union
select T2.id, T2.name, T1.town
from T1
inner join T2 on T2.id = T1.id
select id,name, town from t1
union
select id,name, (select top 1 town from t1 where t1.id=t2.id) from t2