Vlookup in SQl table - mysql

table1
Agent id
A 1
B 2
C 3
D 4
E 5
table2
Agent id
C 3
D 4
output:
Agent id
A 1
B 2
E 3

Do you want not exists?
select t1.*
from table1 t1
where not exists (select 1 from table2 t2 where t2.agent = t1.agent and t2.id = t1.id)
This brings records of table1 for which there is no corresponding record in table2. I assumed that you want to match on both columns: you might to review that, and adapt the where clause of the subquery accordingly.

Related

How to form a join query in mysql to filter records based on some status condition

Consider I have table t1
id
Another header
1
row1
2
row2
3
row3
4
row4
and another table t2
id
t1_id
status
1
1
PG
2
2
S
3
1
CG
4
1
S
5
3
CG
t1 has one2many relationship with t2.
The t1_id has multiple entries in t2 table because t2's status sequence goes on like
PG > CG >S.
So it can have separate entry for each status it passes through.
Once it reaches status S we should not select that t1_id even if it has any of the previous statuses like PG or CG
I want to do left join like t1 left join t2 based on t1 id
And another condition like t2's status should not have S.
So the expected result should be
t1_id
status
3
CG
4
null
How can I achieve this ? Thanks in advance
First retrieve t1_id where status 'S' exists and ignore those t1_id from query if any other status info for that t1_id is existed in the table.
-- MySQL (v5.7)
SELECT t.id t1_id, p.status
FROM t1 t
LEFT JOIN t2 p
ON t.id = p.t1_id
WHERE NOT EXISTS (SELECT 1
FROM t2
WHERE t1_id = t.id AND status = 'S');
Please check from url https://dbfiddle.uk/?rdbms=mysql_5.7&fiddle=2a38e8a8696b68c15f8e95a28f244c86

SQL how to sum rows from 2 tables but only use type of 1 table?

I have 2 tables like below
Table 1
ID Title Number
001 Student 10
002 Student 20
004 Teacher 25
Table 2
ID Title Number
001 Researcher 10
002 Student 20
004 Professor 25
I want to merge the 2 tables such that the number of the same ID are added together but the Title follows that of table 1, so an output like
ID Title Number
001 Student 20
002 Student 40
004 Teacher 50
Thanks.
Try This.
SELECT t1.ID, t1.Title, (t1.number + t2.number) as total
FROM table1 t1 JOIN table2 t2
ON t1.ID= t2.ID
One approach is to begin by doing a UNION ALL of the two tables, and computing the sum of the Number field, grouping on the ID. This result can then be joined back to Table 1 again to use the Title field there.
SELECT t2.ID, t1.Title, t2.Number
FROM
Table1 t1
INNER JOIN
(
SELECT t.ID, SUM(t.Number) AS Number
(
SELECT t1.ID, t1.Number
FROM Table1 t1
UNION ALL
SELECT t2.ID, t2.Number
FROM Table2 t2
) t
GROUP BY t.ID
) t2
ON t1.ID = t2.ID
select a.id,a.title,(a.number+b.number) as number from table1 as a INNER JOIN table2 as b on a.id=b.id;

How to sort list based off of a join to another table

I have a table that needs to be sorted alphabetically by names that aren't actually contained in the table in sqlalchemy. Only codes representing the names are in the table. Like so...
Table 1:
row code month value
1 A 201501 50
2 Z 201501 100
3 CO 201501 200
4 VA 201502 300
5 C 201502 300
Table 2:
row code name
1 A Apple
2 C Cascade
3 CO Colorado
4 VA Virginia
I need to sort Table 1 the following:
month
name (found in Table 2)
What is the best technique to achieve these sorting results when the sorting occurs on value not inherent in the table. I can't send the joined product of Table 1 and Table 2. Although, I can join them temporarily and remove the 'name' column if needed.
Does this work?
SELECT t1.* FROM Table1 t1, Table2 t2 WHERE t1.code = t2.code ORDER BY t2.name;
This will join the tables. But it will return only the content from table t1.
SELECT T1.* FROM TABLE1 T1
INNER JOIN TABLE2 T2 ON
T1.code = T2.code
ORDER BY T1.month, T2.name
Use T2.[columnName] to select data from TABLE2 (if needed)

mysql how to join to table

structure table1:
tb1_id tb1_name
1 a
2 b
3 c
structure for table2:
id name tb1_id
1 a ?
2 b ?
3 c ?
4 a ?
5 a ?
6 b ?
Now lets guess here i have more then 10000 records in table2 and like 1000 records in table1 and i dont know the tb1_id here which i want to update table2 and set tb1_id as it is on table1.
for example it should look like(table2):
id name tb1_id
1 a 1
2 b 2
3 c 3
4 a 1
5 a 1
6 b 2
regards
For MySQL,
update table2 a
inner join table1 b on
a.id=b.tb1_id
set a.tb1_id=b.tb1_id
For SQL Server,
update a
set a.tb1_id=b.tb1_id
from table2 a
inner join table1 b on
a.id=b.tb1_id
Use Update from Join syntax. Try this.
UPDATE table2 AS a
JOIN table1 AS b
ON a.name = b.tb1_name
SET a.tb1_id = b.tb1_id

Mysql Query optimization

Below is my Table Structure
Table 1
id
name
Table 2
id
table1_id
I want the rows from table 1 which have no reference value in table 2.
Example data:
Table 1
id name
1 demo
2 demo2
3 demo3
Table 2
id table1_id
1 1
2 1
3 1
4 3
5 3
So there is no value in table 2 with table1_id 2. I want id 2 from Table 1.
Below id the query i have tried:
SELECT l.id FROM Table1 l WHERE l.id NOT IN (SELECT DISTINCT(r.id) FROM table2 r);
This is returning a proper result but its taking more than 2 minutes to process.
In table 1 i have 4000 rows and in table 2 I have 40000 rows.
Any optimisation to above query or any alternative solution?
SELECT * FROM table1 LEFT JOIN table2
ON table1.id=table2.table1_id
WHERE table2.table1_id IS NULL
Have an index for Table1.id and Table2.table1_id, then try the following query:
SELECT Table1.id FROM Table1
WHERE Table1.id NOT IN (SELECT Table2.id FROM Table2 group by Table2.table1_id);
What you are trying to acheive is to find orphan records right?
A join that shows all the records from the first(the left) table and the matching values form the other or nulls for no matches is called a left join. I think a left join will do the same job but it is not going to be any faster. Joins are in general slower.
I found a place where it is all well explained - http://explainextended.com/2009/09/15/not-in-vs-not-exists-vs-left-join-is-null-sql-server/
It does not hurt to try with a join though, and tell us were your results the same as expected.
select t1.id from table1 as t1
left outer join table2 as t2
on t2.table1_id = t1.id
where t2.id is null;
or
select t1.id from table1 as t1
where not exists (select 1
from table2 as t2 where t2.table1_id = t1.id);