How to replace words in one table for row numbers in another - mysql

I have two tables:
________________________ ________________________________
| id | categoryName | | product | categories |
------------------------ --------------------------------
| 1 | clothes | | coat | clothes,outdoor |
| 2 | indoor | | slippers | clothes,indoor |
| 3 | PPE | | umbrella | outdoor,ppe |
| 4 | outdoor | | | |
------------------------ --------------------------------
I need to replace the values in the categories field in the second table for the category IDs in the first table so the tables would end up looking like this:
________________________ ________________________________
| id | categoryName | | product | categories |
------------------------ --------------------------------
| 1 | clothes | | coat | 1,4 |
| 2 | indoor | | slippers | 1,2 |
| 3 | PPE | | umbrella | 4,3 |
| 4 | outdoor | | | |
------------------------ --------------------------------
Since I have so many to update I am trying to work out how to do it programatically, but am struggling with it.

Related

How to combine multiple rows into a column based on a condition in Mysql?

I have two tables as follows.
Category table
+------------+------------+--------------+
| CategoryID | Name | CategoryCode |
+------------+------------+--------------+
| 1 | Fixed | |
| 2 | Consumable | |
| 3 | Intangible | |
+------------+------------+--------------+
Type table
+--------+------------+-------------------------+----------+
| TypeID | CategoryID | Name | TypeCode |
+--------+------------+-------------------------+----------+
| 1 | 1 | Furniture | |
| 2 | 1 | Computers & Peripherals | |
| 3 | 2 | Keyboards | |
| 4 | 2 | Other | |
| 5 | 3 | Software | |
+--------+------------+-------------------------+----------+
The result I want is like this
+------------+------------+------------------------------------+
| CategoryID | Category | Types |
+------------+------------+------------------------------------+
| 1 | Fixed | Furniture, Computers & Peripherals |
| 2 | Consumable | Keyboards, other |
| 3 | Intangible | Software |
+------------+------------+------------------------------------+
Appriciate if you could help me with wirting the query in MySQL
You can try this solution
SELECT
a.CategoryId,
a.`Name` Category,
GROUP_CONCAT(b.`Name`) Types
FROM
Category a
INNER JOIN Type b ON b.CategoryID = a.CategoryId
GROUP BY a.CategoryId

MySQL Events in information system into programming language

For example I have 3 tables:
Users:
+----+-------------+
| id | nickname |
+----+-------------+
| 1 | Don2Quixote |
| 2 | Pechenye |
| 3 | Maryana |
| 4 | Luman |
| 5 | Tester |
+----+-------------+
Matches:
+----+----------+---------------------+
| id | owner_id | match_end_timestamp |
+----+----------+---------------------+
| 1 | 1 | 1610698498 |
| 2 | 2 | 1610699911 |
| 3 | 3 | 1610701672 |
| 4 | 1 | 1610711211 |
| 5 | 1 | 1610725289 |
+----+----------+---------------------+
matches_players:
+----------+------+-------------+---------+
| match_id | team | slot_number | user_id |
+----------+------+-------------+---------+
| 1 | 1 | 1 | 1 |
| 2 | 1 | 2 | 2 |
...........................................
| 3 | 1 | 1 | 3 |
+----------+------+-------------+---------+
match_end_timestamp is future timestamp. My goal is to get event in programming language on reaching this date and send notification to each player in table matches_players where match_id is id of just ended match. Is it possible? Or the only solution is to store additional timer in my programming language in RAM?

How to create a MySQL view to show all paths for each node in a nested set model through a related table?

I have two tables, categories and products. Categories table is a nested set model. Products table has a serial_number field that is unique. Their schema is like this :
Categories :
+----+-----------+-----+-----+-------+-------------+
| id | parent_id | lft | rgt | depth | title |
+----+-----------+-----+-----+-------+-------------+
| 1 | Null | 2 | 9 | 0 | Cloth |
| 2 | 1 | 3 | 6 | 1 | Men's |
| 3 | 2 | 4 | 5 | 2 | Suits |
| 4 | 1 | 7 | 8 | 1 | Women's |
| 5 | Null | 10 | 13 | 0 | Electronics |
| 6 | 5 | 11 | 12 | 1 | TVs |
+----+-----------+-----+-----+-------+-------------+
Products :
+-------------+---------------+
| category_id | serial_number |
+-------------+---------------+
| 3 | 5461354631 |
| 3 | 4521516545 |
| 4 | 8513453217 |
| 6 | 1235624165 |
+-------------+---------------+
What I want is to create a view to show all serial_numbers with their category path :
+---------------+-------------------+
| serial_number | path |
+---------------+-------------------+
| 5461354631 | Cloth/Men's/Suits |
| 4521516545 | Cloth/Men's/Suits |
| 8513453217 | Cloth/Women's |
| 1235624165 | Electronics/TVs |
+---------------+-------------------+
What is the best query to generate this view?

MYSQL: Select ids from table where multiple values match a single column at least twice

I've got a table that looks like this:
+----+--------------------------------+
| id | slug |
+----+--------------------------------+
| 1 | gift |
| 1 | psychological-manipulation |
| 1 | christmas |
| 1 | giving |
| 1 | the-town-santa-forgot |
| 1 | santa-claus |
| 1 | mp3 |
| 1 | christmas |
| 2 | entertainment-culture |
| 2 | christmas |
| 2 | culture |
| 2 | literature |
| 2 | christmas-music |
| 2 | christmas-window |
| 2 | broadcasting-nec |
| 2 | how-the-grinch-stole-christmas |
| 2 | the-polar-express |
| 2 | banker |
| 2 | christmas |
| 2 | potter |
| 2 | christmas-eve |
| 2 | bailey |
| 2 | its-a-wonderful-life |
| 2 | the-polar-express |
| 2 | disney |
| 2 | tim-burton |
| 2 | a-christmas-carol |
| 2 | the-nightmare-before-christmas |
| 2 | chuck-jones |
+----+--------------------------------+
I want to get unique ids from the table where at least two of a list of slugs match for a given id.
For example lets say I've got the slugs values of:
gift
christmas
giving
I would want all unique ids that have a matching record for at least 2 of those.
i.e. only an id that had both the gift AND christmas slug or the giving AND christmas slug or the gift AND giving slug, etc...
You can use the distinct modifier to count the number of different slugs per ID:
SELECT id
FROM mytable
WHERE slug IN ('gift', 'christmass', 'giving')
GROUP BY id
HAVING COUNT(DISTINCT slug) >= 2

insert into a table having table name derived from subquery

I have tables named Student_Info table, Subject_Info table and an individual table belonging to each student containing information about each subject parameter for that student (like name of the subject, marks he got in that subject etc).
Student_Info
+----+----------+
| Id | Name |
+----+----------+
| 1 | Sam |
| 2 | Taylor |
| 3 | Rick |
+----+----------+
Subject_Info
+---------------+
| Subject_Name |
+---------------+
| Physics |
| Chemistry |
| Mathematics |
+---------------+
1_Info
+---------------+-------+
| Subject_Name | Marks |
+---------------+-------|
| Physics | 60 |
| Chemistry | 40 |
| Mathematics | 80 |
+---------------+-------+
2_Info
+---------------+-------+
| Subject_Name | Marks |
+---------------+-------|
| Physics | 70 |
| Chemistry | 50 |
| Mathematics | 60 |
+---------------+-------+
3_Info
+---------------+-------+
| Subject_Name | Marks |
+---------------+-------|
| Physics | 70 |
| Chemistry | 70 |
| Mathematics | 70 |
+---------------+-------+
Here the tables 1_Info, 2_Info, 3_Info are the tables corresponding to students with Id = 1, Id = 2, Id = 3 respectively.
My questions are
If I insert a subject name into the Subject_Info table I must insert the same subject name in all the individual tables corresponding to each student (1_Info, 2_Info, 3_Info).
I used the following query to get the table name; it works fine.
SELECT CONCAT(Id, '_Info') FROM Student_Info;
I tried to write it using a single query like this
INSERT INTO (SELECT CONCAT(Id, '_Info') FROM Student_Info) VALUES ('Physics');
But getting error.
Is it possibles to do this in single query?
If yes then where am I doing mistake?
Is it a good way to give names to the tables that I am using here for each students subject related parameters (1_Info, 2_Info etc)? If not please suggest a good way.
I think it would be more easy to put the information in 1 Table, like this for example
Student
+----+--------+
| Id | Name |
+----+--------+
| 1 | Sam |
| 2 | Taylor |
| 3 | Rick |
+----+--------+
Subject
+----+-------------+
| Id | Subject |
+----+-------------+
| 1 | Physics |
| 2 | Chemistry |
| 3 | Mathematics |
+----+-------------+
Info
+------------+------------+-------+
| student_id | subject_id | Marks |
+------------+------------+-------+
| 1 | 1 | 60 |
| 1 | 2 | 40 |
| 1 | 3 | 80 |
| 2 | 1 | 70 |
| 2 | 2 | 50 |
| 2 | 3 | 60 |
| 3 | 1 | 70 |
| 3 | 2 | 70 |
| 3 | 3 | 70 |
+------------+------------+-------+
And the INSERT would be
INSERT INTO Student(Name) VALUES('Tom');
INSERT INTO Subject(Subject) VALUES('History');
INSERT INTO Info(4, 4, 80);
EDIT: Alternative, this is what I think is called "Third Normal Form (3NF)"
Student
+----+--------+
| Id | Name |
+----+--------+
| 1 | Sam |
| 2 | Taylor |
| 3 | Rick |
+----+--------+
Subject
+----+-------------+
| Id | Subject |
+----+-------------+
| 1 | Physics |
| 2 | Chemistry |
| 3 | Mathematics |
+----+-------------+
Marks
+----+------+
| Id | Mark |
+----+------+
| 1 | 60 |
| 2 | 40 |
| 3 | 80 |
| 4 | 70 |
| 5 | 50 |
+----+------+
Info
+------------+------------+---------+
| student_id | subject_id | mark_id |
+------------+------------+---------+
| 1 | 1 | 1 |
| 1 | 2 | 2 |
| 1 | 3 | 3 |
| 2 | 1 | 4 |
| 2 | 2 | 5 |
| 2 | 3 | 1 |
| 3 | 1 | 4 |
| 3 | 2 | 4 |
| 3 | 3 | 4 |
+------------+------------+---------+