Retrieving hierarchical data from MySQL table - mysql

Target platform: MySQL 5.7
I have table categories that represents hierarchical categories data:
+----+-----------------+-----------+
| id | name | parent_id |
+----+-----------------+-----------+
| 1 | First category | NULL |
| 2 | Second category | 1 |
| 3 | Third category | 2 |
| 4 | Other category | 1 |
+----+-----------------+-----------+
Also, I have another table (categories_relations) that stores relations between category and all other related (or connected) categories:
+----+-----------+-------------+
| id | parent_id | relation_id |
+----+-----------+-------------+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 1 | 3 |
| 4 | 1 | 4 |
| 5 | 2 | 2 |
| 6 | 2 | 3 |
| 7 | 3 | 3 |
| 8 | 4 | 4 |
+----+-----------+-------------+
Is it possible to retrieve filtered categories (for example - by ID) with all related other categories? For example, if I would query category with ID=1, query result should be:
+----+-----------------+-----------+
| id | name | parent_id |
+----+-----------------+-----------+
| 1 | First category | NULL |
| 2 | Second category | 1 |
| 3 | Third category | 2 |
| 4 | Other category | 1 |
+----+-----------------+-----------+
If ID=4:
+----+-----------------+-----------+
| id | name | parent_id |
+----+-----------------+-----------+
| 1 | First category | NULL |
| 4 | Other category | 1 |
+----+-----------------+-----------+
And so on. Thank you.

Related

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: count if value from one table exists in multiple columns in another table

wondering if anyone one can assist,
I have a fixed reference table
tblConfig_qC
----------------
| id | value |
----------------
| 1 | optionA |
| 2 | optionB |
| 3 | optionC |
| 4 | optionD |
| 5 | optionE |
| 6 | optionF |
| 7 | optionG |
----------------
And a tblSubmission table where I have 3 columns which stores which of the option users selected (from the tblConfig_qC table)
User can select between 1 to 3 options
So typically, rows can be like:
tblSubmissions
------------------------------------
| id | qC1 | qC2 | qC3 |
------------------------------------
| 1 | optionB | | |
| 2 | optionA | optionD | optionE |
| 3 | optionC | optionD | optionF |
| 4 | optionD | optionF | |
------------------------------------
What i need to do is create a summary of counts:
-------------------------
| id | Options | Counts |
-------------------------
| 1 | optionA | 0 |
| 2 | optionB | 1 |
| 3 | optionC | 1 |
| 4 | optionD | 3 |
| 5 | optionE | 1 |
| 6 | optionF | 2 |
| 7 | optionG | 0 |
-------------------------
I can do this for one column but how do i add the counts of the other 2?
SELECT q.value AS 'Option',
COUNT(s.qC1) AS 'qC1',
FROM tblConfig_qC q
LEFT JOIN tblSubmission s ON q.value = s.qC1
group by q.value ORDER BY q.value;

Selected Child Categories based on parent categories from another table

I have two tables
rules
With three step hierarchy
|id | name | parent |
|---|-------|--------|
| 1 | A | 0 |
| 2 | B | 0 |
| 3 | A(1) | 1 |
| 4 | A(2) | 1 |
| 5 | B(1) | 2 |
| 6 | A(1.1)| 3 |
| 7 | A(1.2)| 3 |
| 8 | A(2.1)| 4 |
| 9 | B(1.1)| 5 |
| 10| A(3) | 1 |
Subject
|id | date | rules | group |
|---|---------------------|-------|-------|
| 1 | 2016-05-20 18:24:20 | 2 | AQR48 |
| 2 | 2016-05-20 19:31:17 | 5 | AQR52 |
| 3 | 2016-05-21 18:11:37 | 6,7,4 | AQR48 |
I need to get second step rules based on group and ruleid(first step) of subject table data
When group = 'AQR48' and rules.parent=1 result should be
|id | name | parent |
|---|-------|--------|
| 3 | A(1) | 1 |
| 4 | A(2) | 1 |
I tried it like this but with out success.
select rules.id,rules.name,rules.parent from rules left join subject on find_in_set(rules.id,subject.rules) where rules.parent=1 AND subject.group='AQR48'
With this I get output as
|id | name | parent |
|---|-------|--------|
| 4 | A(2) | 1 |
Anyone could help me with this

How to match comma delimited value with other column in mySql

I have following two tables:
user:
+---------+--------------+
| user_id | skills |
+---------+--------------+
| 1 | 1,2,3,5,4,14 |
| 2 | 1,2,3 |
| 3 | 3,4,5 |
| 4 | 1,2 |
+---------+--------------+
pskills:
+-----+--------+------+----------+
| PID | SKILLS | SPLI | status |
+-----+--------+------+----------+
| 1 | 2,4 | 1 | |
| 1 | 1 | 1 | required |
+-----+--------+------+----------+
I want to match values of SKILLS columns of table pskills. Such as if query is done with first row of pskills and join with user table then it will return User ID 1 because SKILLS 2,4 match with user id 1 only. How can i do this easily?
Never store multiple values in one column!
You should normalize your tables like this
**user**
+---------+--------------+
| user_id | skills |
+---------+--------------+
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 1 | ... |
| 2 | 1 |
| 2 | 2 |
| | ... |
+---------+--------------+
**pskills**
+-----+--------+------+----------+
| PID | SKILLS | SPLI | status |
+-----+--------+------+----------+
| 1 | 2 | 1 | |
| 1 | 4 | 1 | |
| 1 | 1 | 1 | required |
+-----+--------+------+----------+