How to get non-matching records from two tables - mysql

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
)

Related

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.

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;

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

Performing JOIN between two tables

I have two tables like this:
table_1
id user_id item_id item_number
15 1 1 7
16 1 2 12
17 1 3 1
18 1 4 0
19 1 5 11
20 5 1 2
21 5 2 2
22 5 3 5
23 5 4 7
24 5 5 1
table_2
id user_id item_id attribute
41 5 1 1
42 1 1 1
43 7 5 1
44 1 4 1
45 1 4 0
I would like to select user_id, item_id and item_number from table_1 and number of rows from table_2 where table_1.user_id = table_2.user_id and table_1.item_id = table_2.item_id.
I have this:
SELECT item_id, item_number, COUNT(attribute) AS number
FROM table_1
LEFT OUTER JOIN table_2 ON table_1.user_id = table_2.user_id
WHERE table_1.user_id='1'
Can you help me?
Expected result:
user_id item_id item_number number
1 1 7 1
1 2 12 0
1 3 1 0
1 4 0 2
1 5 11 0
Try this:
SELECT t1.user_id, t1.item_id, t1.item_number, COUNT(attribute) AS number
FROM table_1 t1
LEFT OUTER JOIN table_2 t2 ON t1.user_id = t2.user_id AND t1.item_id = t2.item_id
WHERE t1.user_id = '1'
GROUP BY t1.user_id, t1.item_id;
you have to use alias as same column names confuses the server to identify which column have to fetch..so you can use this..
SELECT t1.user_id,t1.item_id, t1.item_number, COUNT(attribute) AS number
FROM table_1 t1
LEFT OUTER JOIN table_2 t2 ON t1.user_id = t2.user_id
WHERE t1.user_id='1' GROUP BY t1.user_id, t1.item_id;
Since you don't really need any column values from table_2, I'd go with a sub-query to get the count:
select user_id, item_id, item_number,
(select count(*) from table_2
where table_1.user_id = table_2.user_id
and table_1.item_id = table_2.item_id)
from table_1
WHERE table_1.user_id='1'

MYSQL merge in one column if given column has no value

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