So I have 2 tables.
table 1:
ID CUST_NO
1 51555
2 51556
3 51111
4 44444
5 54878
6 13548
and table 2:
ID CUST_NO
1 51555
2 51556
3 31333
4 97948
5 65488
6 14648
. .....
I know I can use union to get the CUST_NO's that appear in both tables. However, I need to get the list of CUST_NO's that appear in table 1, but not in table 2.
So a result for this should be
51111
44444
54878
13548
I bet this is really easy but I just can't use my head right now, any thoughts?
select t1.CUST_NO
from Table1 t1
left outer join Table2 t2 on t1.CUST_NO = t2.CUST_NO
where t2.CUST_NO is null
select cust_no
from table1
where not exists
(select cust_no from table2 where table2.cust_no = table1.cust_no)
A NOT IN subquery is simplest, though probably not fastest:
SELECT
ID,
CUST_NO
FROM tab1
WHERE CUST_NO NOT IN (SELECT CUST_NO FROM tab2);
Related
I have 2 tables in Mysql. I need to join them somehow to get one value from second table into the first one.
TABLE 1
Day EmployeeId Total EmployeeName
1 2 20 Josh
1 1 20 Mike
2 2 5 Josh
2 1 10 Mike
3 3 5 Eric
TABLE 2
Day EmployeeId Max_Total
1 2 40
1 1 40
2 2 5
2 1 15
I need to get something like TABLE 3
Day EmployeeId Total EmployeeName Max_Total
1 2 20 Josh 40
1 1 20 Mike 40
2 2 5 Josh 5
2 1 10 Mike 15
3 3 5 Eric null
So this Max_Total column needs to be somehow created and populated.
This Day_EmployedId combination is unique in both tables and that should be used somehow to extract values from 2nd table and add it to the first one.
Sometimes first table can have more values, sometimes the second one, but the first one will always be the one that needs to be manipulated/added to.
Any hint will be appreciated. Thanks
You are looking for a left join on two fields:
select t1.*, t2.max_total
from table1 t1 left join
table2 t2
on t1.day = t2.day and t1.employeeid = t2.employeeid;
I would not recommend actually updating table1. You can generate the data as you need it. However, in order for an update to work, you need to add a column to the table first, and then update it.
You need to separate your tasks.
Alter your Table1 to add the column Max_Total
Write UPDATE query to update your Max_Total in your Table1.
Query:
UPDATE t1.Max_Total = t2.Max_Total
SET t1.
FROM Table1 t1
JOIN Table2 t2 ON t1.Day = t2.Day AND t1.EmployeeId = t2.EmployeeId
If you are only concerned about getting a combined result set
SELECT t1.Day, t1.EmployeeId, t1.Total, t1.EmployeeName, t2.Max_Total
FROM Table1 t1
LEFT JOIN Table2 t2 ON t1.Day = t2.Day AND t1.EmployeeId = t2.EmployeeId
For more information on LEFT JOIN, you can study this tutorial.
I have two tables with same column I need to merge that two table like below
Table1
id name
1 test1
4 test7
5 test9
6 test3
Table2
id name
2 test2
3 test5
6 test3
Result
id name
1 test1
2 test2
3 test5
4 test7
5 test9
6 test3
So I need to join/merge the two tables by id and you can see id 6 present in both table I need to override table 2 value and give above result. Kindly help me to solve the issue.
Thank you.
select id,name from table1
union
select id,name from table2 ;
other way
select * from (
select id,name from table1
union
select id,name from table2)temp order by temp.id ;
This will arrange records id wise
UNION will eliminate duplicate record , In your case it's id 6
When you want sorting then must be to create inner query like this
select * from
(
select id,name from table1 t1
union
select id,name from table2 t2
)a order by a.id ;
I have a table in my MySQL database that I need to remove the duplicates from.
My table looks something like this:
unique_id value frequency value_type publication_date
1 6.5 1 2 2014-12-31
2 7.5 3 5 2014-06-04
3 6.5 1 2 2015-07-13
4 8.0 4 3 2010-12-31
Rows 1 and 3 are duplicates except for the publication_date. I need to remove these duplicates but keep the row with the max publication date so for this example I would want to remove row 1 and keep row 3.
So far I've tried this but it's giving me too many results on my test table:
SELECT t.* FROM
(SELECT MAX(publication_date) AS most_recent_date
FROM table_1
GROUP BY `value`,frequency,value_type
) t1
JOIN table_1 t
ON t.publication_date = t1.most_recent_date;
Any help would be appreciated.
Thanks.
You can use JOIN to remove the duplicates as
delete t1 from table_1 t1
join table_1 t2 on t1.value = t2.value
and t1.frequency= t2.frequency
and t1.value_type = t2.value_type
and t1.unique_id <> t2.unique_id
and t1.publication_date < t2.publication_date ;
My SQL Skills are next to none. After looking around for the past 2 hours trying to figure this out I need some help please.
I have 2 tables as below
Table1 Table2
ID | Name Status_id
----------- ----------
1 | Open 1
2 | Closed 2
3 | On-Hold 1
What I would like to do is count the status_id in table 2 and group by the status_id. Then add the Name where the ID matches in the first column.
What I have at the moment is
SELECT status_id, COUNT(*) AS 'num' FROM table2 GROUP BY status_id
This is great so far and returns
1 | 2
2 | 1
What I need to return is
Open | 2
Closed | 1
I hope that is clear. Can anyone help?
Many thanks!
SELECT a.name, COUNT(*) AS num FROM table2 b
INNER JOIN table1 a
ON b.status_id=a.id
GROUP BY status_id
In the case that you want to also have Zero for On-Hold you'd need to do a LEFT join and count the a column from table2 instead of *
SELECT t1.name,
Count(t2.Status_id) AS num
FROM table1 t1
LEFT JOIN table2 t2
ON t1.id = t2.Status_id
GROUP BY t1.name;
DEMO
I have the following mysql table. I have been trying to select all the rows which are related with the row that has B as value in the code column. The relation is based on the value of the column trans_id
id trans_id code amount side
1 1 A 200 left
2 1 B 200 right
3 2 J 100 right
4 2 C 100 right
5 2 B 200 left
6 3 A 630 right
7 3 K 630 left
My Expected Result:
id trans_id code amount side
1 1 A 200 left
3 2 J 100 right
4 2 C 100 right
Could you please tell me what should be the mysql query to achieve this?
Thanks :)
The following query should return the results you want. This uses a select query to return results to the WHERE clause.
SELECT * FROM yourTable
WHERE trans_id IN (
SELECT trans_id FROM yourTable WHERE code='B'
)
AND code!='B'
Your question is unclear, but as far as I understand, you could use a self join like this:
select a.id,
a.trans_id,
a.code,
a.amount,
a.side
from table as a
inner join table as b on (a.id=b.trans_id and b.code='B');
This will return the row with table.id=2:
id trans_id code amount side
2 1 B 200 right
select
t1.*
from
table_name t1
inner join table_name t2 on t1.trans_id = t2.trans_id
where
t2.code = 'B' and
t2.code <> t1.code
If I'm understanding your problem correctly then a subquery would get you what you need.
SELECT * FROM yourTable WHERE id IN (SELECT trans_id FROM yourTable WHERE code='B')