MySQL - How to put a result of Select Query to existing one? - mysql

Let's say i have two tables, Item and Transaction.
Table Item
ID Name
1 Copper Wire
2 Bolt
3 Screw
4 Rubber Band
5 Pipe
Table Transaction
Trans_ID Item_ID Qty
T1 1 1
T1 2 2
T1 3 1
T1 4 2
T2 1 1
T2 3 2
I needs a select query showing a table like this
ID Name Trans_ID QTY
1 Copper Wire t2 1
2 Bolt
3 Screw t2 2
4 Rubber Band
5 Pipe
Is is possible to do it using MySql Select query alone?

Implicit join:
SELECT Item.ID, Name, Trans_ID, QTY
FROM Item, Transaction
WHERE Item.ID=Transaction.ID
(this query will not have the items without transaction)
Or explicit:
SELECT Item.ID, Name, Trans_ID, QTY
FROM Item
LEFT JOIN Transaction
ON Item.ID=Transaction.ID
(this one will, since it is a LEFT JOIN and the "left" is the Item table)

Related

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.

Is there an option to count pairs of words in a list of concatenated words in SQL

I've got a list of transactions with the product that were purchased - I'm trying to see what items are frequently bought together. A transaction can have 1 item purchased or multiple items.
transaction_id | Product Name
1 | A
1 | B
1 | C
2 | A
3 | A
3 | B
I would like to get:
AB: 2
AC: 1
BC: 1
Some transactions can have even 20 items.
I managed to get rid of the transactions that only have 1 product purchased, I tried grouping by transaction_id and concatenating the product name, but that still won't let me count pairs.
You can use a self-join and aggregation:
select t1.product, t2.product, count(*)
from t t1 join
t t2
on t1.transaction_id = t2.transaction_id and
t1.product < t2.product
group by t1.product, t2.product;
Note: This assumes that the rows are not duplicated.

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.

sql querying the price-history for all the items

I have three tables.
Table 1
Transact Item Status
0 A 1
1 A 1
2 B 1
3 C 0
...
Table 2
Item Item Name
A shoe1
B shoe2
C shoe3
...
Table 3
Transact Price
0 23
1 22
2 10
3 50
How do I get the transaction history for ALL the items? It is easy to get it for one particular item by entering the item or its name, but how about for ALL the items? Presumably, I wish to plot the transaction history for every item, for ALL the items?
Thanks!
SELECT t1.* , t2.ItemName , t3.Price
FROM Table1 t1
INNER JOIN Table2 t2 ON t1.Item = t2.Item
INNER JOIN Table3 t3 ON t1.Transact = t3.Transact

SQL query, 2 tables, avoid duplicated results

Table 1
NO_REG - ID
Table 2
NO_REG - BAGS
the problem its that table 1 contains lets say 3 ID, table 2 has the same NO_REG but just 1 row in BAGS
i do
SELECT BAGS, ID FROM TABLE 1 AS T1 INNER JOIN TABLE 2 AS T2 ON T1.NO_REG = T2.NO_REG
I get
NO_REG ID - BAGS
123 999- 2
123 989- 2
123 979- 2
I need
NO_REG ID - BAGS
123 999- 2
123 989- NULL (or 0)
123 979- NULL (or 0)
hope i was clear enough.
You need LEFT JOIN instead of INNER JOIN which retrieves matching values and null for non matching
Just something like
SELECT BAGS, ID FROM TABLE 1 AS T1 LEFT JOIN TABLE 2 AS T2 ON T1.NO_REG = T2.NO_REG