Insert column if 2 column from different table matches - mysql

I have 2 tables:
Table1- contains PhoneNumber|Name
Table2- contains PhoneNumber|Address
I want to create Table3 with PhoneNumber|Name|Address
Table1 and Table2 may have out of order or different amount of entries, therefore table1 will act as main list.
please suggest way forward. using MySQL.

Use CREATE..SELECT with a LEFT JOIN :
CREATE TABLE Table3 AS
SELECT t1.PhoneNumber,t1.name,t2.address
FROM Table1 t1
LEFT JOIN Table2 t2
ON(t1.PhoneNumber = t2.PhoneNumber)

Related

Value from one table on basis of another table

I have two table as table1 and table2 given below:
I want to have the value of only those table_name from table1 which has there id in print_table column in table 2.
I have implemented the following query but it returns only one value:
SELECT * FROM print_tabel_permission_admin WHERE id IN (select print_table from secondary)
Use FIND_IN_SET:
SELECT DISTINCT
t1.table_name
FROM table1 t1
INNER JOIN table2 t2
ON FIND_IN_SET(t1.id, t2.print_table) > 0;
Demo
You should probably move away from storing CSV data in your tables like this. Instead, break out the IDs in table2 onto separate rows. This will make your life easier.

SQL: Update table when the table copying from has duplicate entries

I have a table which contains unique names, call it table1. I have another table which contains the same names but each name occurs several times, call it table2. Now, I want to copy data from table2 to table1 corresponding to the names. And if table2 has multiple records by the same name, I want the corresponding new records to be created in table1.
TABLE1 TABLE2
NAME NAME
A A
B A
C B
D B
Following the little chat int he comments, you could try this:
UPDATE t1
set columnx = t2.columnx
FROM table1 t1
LEFT JOIN table2 t2 on t2.name = t1.name
WHERE t2.name is null
To achieve your full requirements, you may find it more useful to have multiple queries accomplish the one task.

MySQL JOIN from one of the two table based on IF condition

SELECT Table1.Filter, Table1.Condition, Combined.Data FROM Table1
LEFT JOIN
(SELECT Key, Data FROM IF(Table1.Filter, Table2, Table3))) AS Combined
ON Table1.Condition = Combined.Key
I want to create a MySQL View that shows all columns of Table1, and a column from either Table2 or Table3 depending on the field on Table1.Filter.
One simple solution is to LEFT JOIN both Table2 and Table3, with NULL on the column that is not applicable. Is there a way to avoid creating 2 columns?
I cannot UNION Table2 and Table3 as they might contain the same Key.
The following should do what you want:
SELECT t1.Filter, t1.Condition,
COALESCE(t2.Data, t3.Data) as Data
FROM Table1 t1 LEFT JOIN
Table2 t2
ON t1.Filter AND t2.Key = t1.Condition LEFT JOIN
Table3 t3
ON (NOT t1.Filter) AND t3.key = t1.condition;
You cannot have conditionals choosing tables in the FROM. But, you can have conditions in the ON conditions.

SQL - show results from table without values from another table

I need to show only results which are in Table1 and Table2 but are not in Table3. Basically, it should be something like TABLE1, Table2 except INNER JOIN between (TABLE1, Table2) and TABLE3.
Should looks like this - On left side Table1 and Table2, on right side Table3
Now I have this:
SELECT mesta_email, mesta_kod
FROM Table1
UNION ALL
SELECT mesta_email, mesta_kod
FROM Table2
// And somehow except values which are in Table3
Can somebody help me please? Thanks a lot.
There are a couple different ways to do this. I believe mysql does better with the outer join/null approach:
select t.*
from (
SELECT mesta_email, mesta_kod
FROM Table1
UNION ALL
SELECT mesta_email, mesta_kod
FROM Table2
) t left join Table3 t3 on t.mesta_email = t3.mesta_email
and t.mesta_kod = t3.mesta_kod
where t3.mesta_email is null
This assume table3 shares the same structure as the other 2 tables.
I would approach the problem almost directly as you write it, using exists and not exists:
select t1.mesta_email, t2.mesta_kod
from table1 t1
where exists (select 1
from table2 t2
where t2.mesta_email = t1.mesta_email and t2.mesta_kod = t1.mesta_kod
) and
not exists (select 1
from table3 t3
where t3.mesta_email = t1.mesta_email and t3.mesta_kod = t1.mesta_kod
);
One advantage of exists/not exists over other approaches involves duplicates. If one of the tables (say table1) has not duplicates, but the others might, there is no need to remove duplicates in the resulting data set.

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