How to do update with join - mysql

just think I have a table1 like shown below
table1
id product
2 chocolate
1 chocolate
2 pepsi
3 fanta
2 pepsi
4 chocolate
5 chips
3 pizza
1 coke
2 chips
6 burger
7 sprite
0 pepsi
and want to arrange the above table in the manner shown below using only mysql
table2
id product
0 pepsi
1 chocolate, coke
2 chocolate,fanta,chips
3 fanta,pizza
4 chocolate
5 chips
6 burger
7 sprite
the above thing can be done by using
select id, group_concat(distinct product) as products
from table1
group by id
order by id
and after that i want the product column from table2 to be updated in another table which is named table3, which is shown below, here i want to update the products column from table2 as things column in table3 and the number in table3 = id from table2
table3
number name things
0 hi null
1 hello null
2 hehe null
3 wow null
4 hi null
5 hi null
6 hi null
7 hi null
and i want the final output to be shown as in table2
table3
number name things
0 hi pepsi
1 hello chocolate, coke
2 hehe chocolate,fanta,chips
3 wow fanta,pizza
4 hi chocolate
5 hi chips
6 hi burger
7 hi sprite

Use update from select syntax. Check here for more info
UPDATE table3
JOIN (SELECT id,
Group_concat(DISTINCT product) AS products
FROM table1
GROUP BY id) b
ON table3.number = b.id
SET table3.things = b.products

You have a nice normalized data structure and you want to start putting in comma-separated lists. That is a bad idea. Doable, but probably a bad idea.
You would use an update with a join:
update table3 t3 join
(select id, group_concat(distinct product) as products
from table1
group by id
) tt
on t3.number = tt.id
set t3.things = tt.products;

Related

SQL - Count two columns together

I'm trying to count multiple columns as one column. For example:
Table 1 (Books):
ID
BookName
Genre
SubGenre
1
Name1
1
3
2
Name2
2
1
3
Name3
4
2
Table 2 (Genre):
ID
Genre
1
Horror
2
Drama
3
Romance
4
Sci-Fi
I want to be able to count the genre and subgenre as one to create a table of:
Result:
Genre
Count
Horror
2
Drama
2
Romance
1
Sci-Fi
1
Any help would be really appreciated. Thanks
Try below query-
SELECT t.Genre, Sum(t.cg) AS Count
FROM (
SELECT t2.Genre, Count(t1.Genre) AS cg
FROM Table2 as t2 LEFT JOIN Table1 as t1 ON t2.ID = t1.Genre
GROUP BY t2.Genre
UNION ALL
SELECT t2.Genre, Count(t1.SubGenre) AS cg
FROM Table2 as t2 LEFT JOIN Table1 as t1 ON t2.ID = t1.SubGenre
GROUP BY t2.Genre
) as t GROUP BY t.Genre;

How can I select the longest text from my mysql database?

I do clustering for news articles. I classify a number of data into a number of clusters. what I want to do is to take one data from each cluster that has content with the longest text.
I have two table, i want to join that two tables and show only the record with the longest text.
my tables:
Table newscontent
news_id title content category
1 abcd abcd a
2 abcd abcdefg a
3 abcd abcdefghij a
4 efgh efgh a
5 efgh efghijk a
6 efgh efghijklmn a
7 ijkl ijkl b
8 ijkl ijklmn b
Table newscluster
newscluster_id news_id category cluster
1 1 a 0
2 2 a 0
3 3 a 0
4 4 a 1
5 5 a 1
6 6 a 1
Desired output:
news_id title content category cluster
3 abcd abcdefghij a 0
6 efgh efghijklmn a 1
How can i do that?
You can accomplish what you want by using a series of joins. However, I have the feeling that your schema is not completely normalized.
SELECT t2.news_id,
t2.title,
t2.content,
t2.category,
t1.cluster
FROM newscluster t1
INNER JOIN newscontent t2
ON t1.news_id = t2.news_id
INNER JOIN
(
SELECT t1.cluster, MAX(CHAR_LENGTH(t2.content)) AS max_content_length
FROM newscluster t1
INNER JOIN newscontent t2
ON t1.news_id = t2.news_id
GROUP BY t1.cluster
) t3
ON t1.cluster = t3.cluster AND
CHAR_LENGTH(t2.content) = t3.max_content_length
-- WHERE t2.category = 'a'
Try this:
select * from (
select a.*, cluster from newscontent a
join newscluster b on a.news_id =b.news_id
order by length(content) desc) x
group by cluster
Some people will complain, but if it works, it works!

select query get data from main table and all values in child table?

My tables structure is :
Student:
ID Name From
1 student A England
2 student B China
3 student C USA
Subject:
ID id_student Subject
1 1 Maths
2 1 Physics
3 2 English
4 3 Physics
5 4 History
I want to get all data in main table (A) and all rows in have id_A in child table (B) to show in grid table like this :
ID Student Subject
1 student_A Maths, Physics
2 student_B English
3 student_C Physics, History
I wonder how to select data ?
You can use GROUP_CONCAT:
SELECT t1.ID, t1.Name, GROUP_CONCAT(t2.Subject)
FROM Student AS t1
LEFT JOIN Subject AS t2 ON t1.ID = t2.id_student
GROUP BY t1.ID, t1.Name

Select all IDs that have that value in MySQL

I have these two tables:
Table 1:
ID ITEM
--------------------
1 AB
1 22S
1 AB
2 F45
2 BB
3 1
3 1
3 AA
3 F45
3 F67
3 A
......
Table 2:
ITEM COUNTRY
---------------
0 usa
1 italy
2 mexico
3 mexico
A greece
AA malta
AB usa
AC pakistan
B uk
BB france
BA france
BB russia
F45 uk
And I use the following query to get the Items that are like "A%" for each ID:
SELECT GROUP_CONCAT(ITEM)
FROM (SELECT Table1.ID, Table1.ITEM, Table1.date
FROM Table2 Table2 INNER JOIN Table1 Table1
ON (Table2.ITEM = Table1.ITEM) WHERE table2.ITEM like "A%"
ORDER BY Table1.id, Table1.date, Table2.ITEM) as temp
GROUP BY ID
ORDER BY ID
I want to find a way to edit my query so I can get the above only for the IDs that happen to have item "F45" in them (but I don't want to use this item, just select the IDs that have it)..
Any help will be appreciated
I guess
SELECT GROUP_CONCAT(ITEM) AS group_item
FROM (SELECT Table1.ID, Table1.ITEM, Table1.date
FROM Table2 Table2 INNER JOIN Table1 Table1
ON (Table2.ITEM = Table1.ITEM) WHERE table2.ITEM like "A%"
ORDER BY Table1.id, Table1.date, Table2.ITEM) as temp
GROUP BY ID
HAVING group_item LIKE '%F45%'
ORDER BY ID
Or adjust the subquery to select only needed rows (here I'm not sure what are you trying to achieve)

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

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)