Join two table with different columns [duplicate] - mysql

This question already has answers here:
How can I do a FULL OUTER JOIN in MySQL?
(15 answers)
Closed 2 years ago.
I have two table that I want to combine without FULL OUTER JOIN as it not work in h2 db. I have to create select sql query:I have tried much with UNION too but I think it will not be possible with UNION.
Table 1
id c_id s_id p_date
---------------------------------------------
1 1 1 2020-10-10
2 1 1 2020-10-11
3 2 1 2020-10-11
4 2 2 2020-10-12
Table 2
id c_id s_id s_date
---------------------------------------------
1 1 1 2020-10-15
2 1 2 2020-10-16
3 2 2 2020-10-17
4 2 2 2020-10-17
I am expecting below result:
c_id s_id p_date s_date
-------------------------------------------------
1 1 2020-10-10 2020-10-15
1 1 2020-10-11 -
1 2 - 2020-10-16
2 1 2020-10-11 -
2 2 2020-10-12 2020-10-17
2 2 - 2020-10-17
Please help to get this result.

You can use union all like this:
select t1.c_id, t1.s_id, t1.p_date, t2.s_date
from table1 t1 left join
table2 t2
on t1.c_id = t2.c_id and t1.s_id = t2.s_id
union all
select t2.c_id, t2.s_id, t1.p_date, t2.s_date
from table2 t2 left join
table1 t1
on t1.c_id = t2.c_id and t1.s_id = t2.s_id
where t1.c_id is null;
The first subquery gets all the rows where there are matches between the two tables plus rows where table2 has no match for table1.
The second subquery gets the additional rows from table2 that have no match in table1.
Here is a db<>fiddle.

Related

MySql fill up table with values depending on other table

In mySql I have 2 tables.
t1 looks like
id color
------------
1 red
2 green
3 blue
4 purple
t2 looks like
id t1_id ship
------------------------
1 1 a
2 1 b
3 1 c
4 2 a
5 2 b
6 3 b
I need to fill t2 so that for each entry in t1 there are three entries in t2 - one with "a" in the ship row one with "b" in the ship row and one with "c" in it.
I don't want to have multiple entries with the same t1_id <-> ship combo. So after the query t2 should look like this.
t2 after the query I am looking for
id t1_id ship
------------------------
1 1 a
2 1 b
3 1 c
4 2 a
5 2 b
6 3 b
7 2 c
8 3 a
9 3 c
10 4 a
11 4 b
12 4 c
Any ideas on how to achieve that?
If you want to put these in the table, you can use:
insert into t2 (t1_id, ship)
select t1.id, s.ship
from t1 cross join
(select distinct ship from t2) s left join
t2
on t2.t1_id = t1.id and t2.ship = s.ship
where t2.t1_id is null;
This inserts the "remaining" values in table2.

Identical values in one column and unique value pairs

I have 2 columns
A B
1 2
2 2
1 2
3 2
5 2
0 2
4 2
11 4
12 4
11 4
I want the SQL query to return the pairs (A,B) where:
B has appeared 3 or more times
AND (A,B) is unique
The resulting table would be:
A B
1 2
2 2
3 2
5 2
0 2
4 2
You could use a join with a selected table grouped by B having count = 3
select distinct A, B
from my_table as t1
inner join (
select b
from my_table
group by b
having count(*)= 3
) t2 on t2.b = t1.b
and for 3 or more
select distinct A, B
from my_table as t1
inner join (
select b
from my_table
group by b
having count(*) >= 3
) t2 on t2.b = t1.b

MySql get highest count per parent ID

ID item_ID parent_ID count
================================
1 11 2 5
2 12 2 6
3 13 3 2
4 14 3 3
5 15 2 7
6 16 1 3
SELECT * FROM relations ORDER BY count DESC
The row that should be returns are 2,4 and 6 because they have the highest count for their parent_ID
how do i change the query to accomplish this?
The inner select gets the highest count for each parent_ID. If you join against that, it filters out the relevant records
select t1.*
from your_table t1
join
(
select parent_ID, max(count) as mcount
from your_table
group by parent_ID
) t2 on t1.parent_ID = t2.parent_ID
and t1.count = t2.mcount

Get latest result in SQL [duplicate]

This question already has answers here:
Returning the 'last' row of each 'group by' in MySQL
(6 answers)
Closed 7 years ago.
I have following table structure:
id customer_id version_num
1 1 1
2 1 2
3 2 1
4 2 2
5 3 1
6 3 2
7 3 3
I want results to be displayed as:
id customer_id version_num
2 1 2
4 2 2
7 3 3
I'm able to get the following results:
id customer_id version_num
1 1 1
3 2 1
5 3 1
Query that I used:
select * from TABLE group by customer_id
I don't know how to make use of latest(version_num) in my query.
SELECT Y.id, Y.customer_id, Y.version_num
FROM YourTable Y
JOIN (SELECT customer_id, MAX(version_num) max_v
FROM YourTable
GROUP customer_id
) T
ON Y.customer_id = T.customer_id
AND Y.version_num = T.max_v;

MySql subqueries and max or group by?

I have this table:
ID STUDENT CLASS QUESTION ANSWER TIME
1 1 1 1 c 12:30
2 1 1 1 d 12:36
3 1 1 2 a 12:38
4 2 1 1 b 11:24
5 2 1 1 c 11:26
6 2 1 3 d 11:35
7 2 3 3 b 11:24
I'm trying to write a query that does this:
For each STUDENT in a specific CLASS select the most recent ANSWER for each QUESTION.
So, choosing class "1" would return:
ID STUDENT CLASS QUESTION ANSWER TIME
2 1 1 1 d 12:36
3 1 1 2 a 12:38
5 2 1 1 c 11:26
6 2 1 3 d 11:35
I've tried various combinations of subqueries, joins, and grouping, but nothing is working. Any ideas?
You can use a sub-query to get most recent ANSWER per QUESTION, Then use this as a derived table and join back to the original table:
SELECT m.*
FROM mytable AS m
INNER JOIN (
SELECT STUDENT, QUESTION, MAX(`TIME`) AS mTime
FROM mytable
WHERE CLASS = 1
GROUP BY STUDENT, QUESTION
) AS d ON m.STUDENT = d.STUDENT AND m.QUESTION = d.QUESTION AND m.`TIME` = d.mTime
WHERE m.CLASS = 1
Demo here