MYSQL merge in one column if given column has no value - mysql

How can I merge two column in ONE column if the given column (year_id) has no value (null)
table 1
id txt year_id date
----------------------------------
1 text1 1
2 text2 2
3 text3 3
4 text4 2013-01-02
5 text5 2013-01-03
table 2
id year
----------------
1 2009
2 2010
3 2011
4 2012
I need a result like this
id txt merge_column
-------------------------
1 text1 2009
2 text2 2010
3 text3 2011
4 text4 2013-01-02
5 text5 2013-01-03
thank you in advance, this query complicates my mind.. thank you

JOIN both table first the use COALESCE() or IFNULL().
SELECT a.id,
a.txt,
COALESCE(b.year, a.date) merge_column
FROM table1 a
LEFT JOIN table2 b
ON a.year_id = b.id
SQLFiddle Demo

SELECT t1.id, txt, IFNULL(date, year) merge_column
FROM table1 t1
LEFT JOIN table2 t2 ON t1.year_id = t2.id

You are going to need to convert the values to a common type. I think string is the most likely.
select t1.id, t1.txt,
coalesce(date_format(t2.date, '%y-%m-%d'), cast(t2.year as varchar(32))
from table1 t1 left join
table2 t2
on t1.year_id = t2.id

Related

select the min value of a id from other table

I want to select the min value of a id from other table
here my table1
id
grades
1
1
2
2
1
3
1
4
2
5
my table2
id
name
1
andy
2
lucy
3
kevin
I tried this
select table2.id, name, min(table1.grades) as grade from table1, table2
but it only shows 1 record
what I expected
id
name
grade
1
andy
1
2
lucy
2
3
kevin
0(or null?)
thanks
SELECT id, table2.name, MIN(table1.grades) grade
FROM table1
JOIN table2 USING (id)
GROUP BY 1,2;

Join two table with different columns [duplicate]

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.

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.

Swap column values across 2 different tables

I have two tables and need to swap the values of a column in each table - I can do this when they are in the same table but when I try to do this with different tables then the second value is already overwritten so gets lost.
For example:
table1
id user_id currency col2 col3......
1 1 10 Bob 2018-04-16
2 2 150 Tom 2018-05-17
3 3 60 Phil 2018-06-04
4 4 125 Jon 2017-12-01
5 5 35 Mike 2018-07-21
table2
id user_id salary col2 col3......
1 1 USD 16 Active
2 2 USD 17 Active
3 3 GBP 21 Left
4 4 CAD 16 Active
5 5 AUD 19 Active
I need these to look like:
table1
id user_id currency col2 col3......
1 1 USD Bob 2018-04-16
2 2 USD Tom 2018-05-17
3 3 GBP Phil 2018-06-04
4 4 CAD Jon 2017-12-01
5 5 AUD Mike 2018-07-21
table2
id user_id salary col2 col3......
1 1 10 16 Active
2 2 150 17 Active
3 3 60 21 Left
4 4 125 16 Active
5 5 35 19 Active
I tried:
UPDATE table1 t1, table2 t2
SET t1.currency=t2.salary, t2.salary=t1.currency
WHERE t1.user_id=t2.user_id;
but this does not work (currency gets set correctly but not the salary), is it possible to do?
Swap two columns values between two tables looked like a possible solution but the solution is changing table names as all the columns need swopped whereas I only need single columns swapped.
I believe you'll need to use a mix of both DDL and DML to do this.
First off you'll need to rename one of the columns to be swapped and add a column to hold the new value:
alter table table1 change currency salary int;
alter table table1 add currency varchar(3) after salary;
then update each table independently:
update table1 t1, table2 t2
set t1.currency = t2.salary
where t1.user_id = t2.user_id;
update table1 t1, table2 t2
set t2.salary = t1.salary
where t1.user_id = t2.user_id;
and finally remove the extra column:
alter table table1 drop salary;

How to get non-matching records from two tables

See these two sample tables:
Table 1:
id acc_no name
------------------------
1 14 aaaa
2 16 bbbb
3 18 ccccc
4 25 wwww
5 27 xxxxxxx
6 28 zzzzzzz
Table 2:
sr no acc_no amount
----------------------
1 14 2000
2 16 2344
3 18 3200
I need to get records on basis of acc_no which are not matching in table 1 for example:
OUTPUT:
id acc_no name
---------------------
4 25 wwww
5 27 xxxxxxx
6 28 zzzzzzz
When I tried with below query ,the result was not reliable:
SELECT t1.*
FROM table1 t1
LEFT OUTER JOIN table2 t2 ON t1.acc_no = t2.acc_no
WHERE t2.acc_no IS NULL
Give your suggestions. What will be right SQL query ti get above output?
try:
SELECT *
FROM table1 t1
WHERE t1.acc_no NOT IN (SELECT acc_no FROM table2)
Should be :
select t1.id,t1.acc_no,t1.name from table1 t1
left outer join table2 t2 on t1.acc_no = t2.acc_no
where
t2.id is null
Try this one also:
select t1.* from table1 t1 where
not exists (
select 1 from table2 t2
where t1.acc_no=t2.acc_no
)