Joins and restrictions for different columns at the same time - mysql

Can we use a join operation and make restrictions for different columns at the same time? I want to create a table on a Database (called DB1 in the example) based on three tables from another database (called DB2 in the example) where some columns in the new table filled with entrys when there´s a specific entry in an other column (in the example a"4" in column "attributeID" at table 2 indicates an entry in column "gender", a "5" indicates an entry in column "age"; a"7" in column "attributeID"at table 3 indicates an entry in column "street"). -> If yes, then how to do ?
Both databases are on the same server and DBMS is the same. ID1 and ID2 based on table1 in DB2; ID2, attributeID and value (where the gender or age is written depending on entry in attributeID) based on table2 in DB2; ID2 attributeID and value (where the street is written when the entry in the attributeID is a 7)based on table3 in DB2 .
Sample Data:
Table 1:
ID1 ID2
1 2
2 4
3 5
Table 2:
ID2 attributeID value
2 3 Kahtrin ->an example entry which is not relevant
2 4 miss
2 5 22
4 1 active ->an example entry which is not relevant
4 4 EMPTY/NULL
4 5 47
5 4 mr
5 5 34
5 6 Hindu ->an example entry which is not relevant
Table 3
ID2 attributeID value
2 5 20% ->an example entry which is not relevant
2 7 middlestreet 40
4 4 chinese ->an example entry which is not relevant
4 7 churchstreet 12
5 3 3Euro
5 7 EMPTY/NULL
Expected Outcome
Table 4:
ID1 ID2 gender age street
1 2 miss 22 middlestreet 40
2 4 47 churchstreet 12
3 5 mr 34
Here´s what I tried out (Made from point of view that I´m using DB1):
INSERT INTO table4
(id1,
id2,
gender,
age,
street)
SELECT t1.id1,
t1.id2,
t2.value
t2.value
t3.value
FROM db1.table1 t1
LEFT JOIN db1.table2 t2
ON t1.ID2=t2.ID2
AND value = 4 OR 5
LEFT JOIN db1.table3 t3
ON t1.ID2=t3.ID2
AND value = 7;

You select is wrong (don't produce the expected result ) you should use two time the table t2 using an alias
and you should explicit assign the table t3 to value
SELECT t1.id1,
t1.id2,
t2.value
t4.value
t3.value
FROM db1.table1 t1
LEFT JOIN db1.table2 t2
ON t1.ID2=t2.ID2
AND value = 4
LEFT JOIN db1.table2 t4
ON t1.ID2=t4.ID2
AND value = 5
LEFT JOIN db1.table3 t3
ON t1.ID2=t3.ID2
AND t3.value = 7;

Related

Identify missing conversions with an SQL query

I need to find a way to find what conversions are missing.
I have three tables.
Table 1: type, which are the different types.
id
name
1
typeA
2
typeB
3
typeC
Table 2: section, which are the different sections.
id
name
1
section1
2
section2
3
section3
4
section4
Table 3: conversions, which contains all the combinations to go from one type to another for each of the sections.
id
section_id
type_convert_from
type_convert_to
1
1
1
2
2
2
1
2
3
3
1
2
4
4
1
2
5
1
1
3
6
2
1
3
7
3
1
3
8
4
1
3
9
1
2
1
10
2
2
1
11
3
2
1
12
4
2
1
For example some are missing from the table above, how can I identify them with a SQL query? Thanks!
Try this. The cross join of table type with itself generates all possible combinations of type id's. I've excluded combinations in the cross join where id_from = id_to (ie you're not interested in conversions from a type to itself)
select * from conversions C
right join (
select T1.id as id_from, T2.id as id_to
from type T1 cross join type T2
where T1.id <> T2.id
) X on X.id_from = C.type_convert_from and X.id_to = C.type_convert_to
where C.type_convert_from is null
If you want to check missing type conversions by section, extend the cross join by adding the section table to include section.id as follows. It will list missing type conversions within each section.
select X.section_id, X.id_from, X.id_to from conversions C
right join (
select S.id as section_id, T1.id as id_from, T2.id as id_to
from types T1 cross join types T2 cross join section S
where T1.id <> T2.id
) X
on X.id_from = C.type_convert_from and X.id_to = C.type_convert_to
and C.section_id = X.section_id
where C.type_convert_from is null

how to remove duplicate entries from many to many relation using sql query

My tables look like this. my op and country is having many to many relationships with each other.
OP
id, name,.....
op_country
id, op_id, country_id
country
id, name, ...
my op_country filled like below
id op_id country_id
1 1 1
2 1 2
3 2 2
4 2 3
5 3 3
6 3 3
7 1 1
I want to remove my duplicate entries from op_country. Here I want to remove rows 6 and 7 since we already have rows with such values.
How can I do that.
DELETE t1
FROM op_country t1
JOIN op_country t2 USING (op_id, country_id)
WHERE t1.id > t2.id
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=247ebc5870a6ab10b64076ffb375797f
You want to delete entries for which exists a sibling with a lower ID:
delete from op_country
where exists
(
select null
from (select * from op_country) op2
where op2.op_id = op_country.op_id
and op2.country_id = op_country.country_id
and op2.id < op_country.id
);
The from (select * from op_country) is necessary instead of a mere from op_country due to some weird restriction in MySQL updates.

SQL Query Difference between Table 1 compared to Sum of Table2 Child Items Referenced by Foreign Key

Let's say I have 2 tables:
Table1
ID Name Quantity
1 Pencil 12
2 Pen 35
3 Ruler 50
Table 2
ID Name Quantity FK_Table1
1 Pencil 6 1
2 Pencil 6 1
3 Pen 10 2
4 Pen 5 2
5 Pen 5 2
6 Pen 5 2
7 Ruler 12 3
8 Ruler 12 3
9 Ruler 12 3
10 Ruler 12 3
I need to make a query that selects only the row in table1 from quantity column where it doesn't match with the sum of all quantity column in Table2 that are having the same FK_Table1 value
(e.g. Pencil will not be selected since its quantity is 12 in table1, the same as the sum of quantities of row ID# 1 and 2 in table2).
I need to make a query that selects the table2 rows instead of table1, but with same rules applied
How can I do this?
Try below query :
select * from table1 t1
where t1.Quantity <> (select sum(t2.Quantity) from table2 t2 where t2.FK_Table1 = t1.ID);
For getting records of Table 2:
select * from table2 where FK_Table1 IN(
select t1.ID from table1 t1
where t1.Quantity <> (
select sum(t2.Quantity) from table2 t2 where t2.FK_Table1 = t1.ID
)
);

Sql join, extract value from the second table and add it to the first one

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.

mysql update an column from another table

Table1:
id name value
===================
1 Jane 28
2 Joe 35
Table2:
id name value integer
=========================
1 Jane 28 379
2 Joe 30 325
2 Joe 32 380
1 Jane 28 385
Table1 contains information about user: ID, Name and Value.
Table2 contains information about changes: ID, Name, Value and Integer.
Integer is a value thats always increase and changes about twice a day.
I want to transfer the value from table1 for a specific ID and that value to table2 and put that value at the last line.
In this example I want
ID: 2
Value: 35
in Table1 to relace
ID: 2
Value: 32
in Table2. And I need to use the Integer in some way..
If interger the column which tells the latest in the table2 then you can use the update join as
update table1 t1
join table2 t2 on t1.id = t2.id
join (
select max(`integer`) as `integer`,id
from table2
group by id
)x
on x.id = t2.id and x.`integer` = t2.`integer`
set t1.value = t2.value
where t1.id = 2 ;