compare one column in table with many columns in another table mysql - mysql

I want to compare a column in tbl1 with more than columns in tbl2
to find the matched values my code with one column in tbl2
SELECT * FROM tbl1 WHERE new_pass1 IN(SELECT pass_no1 FROM tbl2)
this code compare with on column in the tbl2
I want with more than column pass_no2 and pass_no3 in tbl2

Just add the other columns to your SELECT:
SELECT *
FROM tbl2
WHERE new_pass1 IN(SELECT pass_no1 FROM tb2) OR
new_pass1 IN(SELECT pass_no2 FROM tb2) OR
new_pass1 IN(SELECT pass_no3 FROM tb2)

Related

Query a table with limited results, based on another table

Let's assume I have tbl2, with a foreign key to another table (tbl1). For each record in the tbl1, we can have multiple or no records in tbl2.
I want to have only one record from tbl2 (the last record, based on time), which matches with a record on tbl1. The following query only returns one record:
select * from tbl2
where fk in (select id from tbl1 where some_criteria)
order by time LIMIT 1 DESC
This query also returns all records from tbl2:
select * from tbl2
where fk in (select id from tbl1 where some_criteria)
order by time DESC
I wanna have a row for each record from select id from tbl1 where some_criteria, having all details from the latest record exists in tbl2.
You want a lateral join, available since MySQL 8.0.14:
select *
from tbl1
left outer join lateral
(
select *
from tbl2
where tbl2.fk = tbl1.id
order by time desc
limit 1
) newest_tbl2 on true;
Here is a solution for old MySQL versions: Aggregate the tbl2 by fk to get the maximum time per fk. Then use this result in your joins.
select *
from tbl1
left outer join
(
select fk, max(time) as max_time
from tbl2
group by fk
) mx on mx.fk = tbl1.id
left outer join tbl2 on tbl2.fk = mx.fk and tbl2.time = mx.max_time;

mysql select from table 1 where no match in table 2

I have two tables, both contain email address.
I need to return all rows in table 1 that do not have a matching email address in table 2.
For simplicity sake we can just say they both have two fields, name and email.
select * from table1 where emailAddress not it (select email address from table2)
You can try LEFT JOIN with NULL in where clause. In LEFT JOIN, if table2 has no matching values, it will be represented by NULL.
SELECT * FROM Table1 LEFT JOIN Table2 ON Table1.email = Table2.email WHERE Table2.email IS NULL
SELECT
table_A.column_1,
table_A.column_2,
table_A.email
FROM table_A
WHERE table_A.email
NOT IN ( SELECT table_B.email FROM table_B )
An example with several columns from Table A.
Both, Table A and Table B have a column named "email".
Matching emails from Table B will be omitted in the query results.
( This question is similar to Mysql: Select rows from a table that are not in another )

I wanted to know the command to check if all the values in one field of a table is present in another table under a different field name

I have 2 tables. I want to find out whether the values present in the first table is there in another table with a different field name.
Here is how it looks,
Table1
BillNo
43529179
43256787
35425676
25467778
24354758
45754748
Table2
BNo
113104808
25426577
268579679
2542135464
252525232
235263663
I have 137 records in table1 that needs to be checked against table2.
Instead of doing it one by one using the following command,
Select * from Table2 where BNo = '43529179';
This gives the result for just the mentioned value. Is there a way to check for all the values in a single query?
Thanks!
You can use a sub-select to compare against:
Select * from Table2 where BNo IN (SELECT BillNo FROM Table1);
That will "evalaute" to something like Select * from Table2 where BNo IN (113104808, 25426577, 268579679, 2542135464, 252525232, ...);
Join the tables, and check how many matching records there are:
select
count(*) as Matches
from
Table1 as t1
inner join Table2 as t2 on t2.BNo = t1.BillNo
You can also use a left join to pick out the records in table 1 that has no matching record in table 2:
select
t1.BillNo
from
Table1 as t1
left join Table2 as t2 on t2.BNo = t1.BillNo
where
t2.BNo is null

Insert non-duplicate rows from one table to another mysql

I have two tables T1 and T2. I want to merge only those rows of T2 which is currently not there in T1 .
`Insert into T1 select * from (select * from T2 where id not in (select id from T1))`
Is there a better and faste way of achieving the same. ID column is unique across the table
Insert into T1
select * from T2
where id not in (select id from T1)
You could also join but then you'd need another subselect since MySQL does not want to select from a table it inserts at the same time without using a subselect.

MySQL: UPDATE table with COUNT from another table?

I thought this would be simple but I can't get my head around it...
I have one table tbl1 and it has columns id,otherstuff,num.
I have another table tbl2 and it has columns id,info.
What I want to is make the num column of tbl1 equal to the number of rows with the same id in tbl2. Kind of like this:
UPDATE tbl1 SET num =
(SELECT COUNT(*) FROM tbl2 WHERE id=tbl1.id)
Any ideas?
If your num column is a valid numeric type your query should work as is:
UPDATE tbl1 SET num = (SELECT COUNT(*) FROM tbl2 WHERE id=tbl1.id)
UPDATE tbl1, (select id, count(*) as idCount from tbl2 group by id) as t2
SET tbl1.num = t2.idCount
WHERE tbl1.id = t2.id;