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

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?

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?

Retrieving hierarchical data from MySQL table

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.

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

Select once for duplicate record

This is my tables which I need get data from these tables. Each user can under many type of groups for example 2, 3, 4
1)groups
|gp_id|gp_name|gp_status|
-------------------------------------
| 1 | Admin | Active |
| 2 | R&D | Active |
| 3 | Sales | Active |
| 4 | IT | Active |
2)modules (FK of table parent_modules)
|mod_id| mod_name| pmod_id |
---------------------------------------------
| 1 | name1 | 1 |
| 2 | name2 | 1 |
| 3 | name3 | 2 |
| 4 | name4 | 3 |
| ... | name... | 3 |
mod_id = 5,6,7.. and so on
3)parent_modules
|pmod_id| mod_name |pmod_status|
-----------------------------------------
| 1 | Contact Us | Active |
| 2 | Account Settings | Active |
| 3 | System Settings | Active |
4)group_details
(FK of table groups) (FK of table modules)
|gpd_id | gp_id | mod_id | base_gpd_rule_view/edit/update/insert |
-----------------------------------------------------------------------
| 1 | 3 | 1 | on |
| 2 | 3 | 2 | on |
| 3 | 3 | 3 | |
| 4 | 3 | 4 |
| 5 | 3 | 5 | on |
| 6 | 3 | 6 |
| 7 | 3 | 7 |
| 8 | 4 | 6 |
| 9 | 4 | 7 | on |
| 10 | 4 | 8 | on |
I have to select once only of duplicate results from group_details and this is the result that I want.
| gp_id | mod_id |
----------------------------------------------
| 3 | 1 |
| 3 | 2 |
| 3 | 3 |
| 3 | 4 |
| 3 | 5 |
| 3 | 6 |
| 3 | 7 |
| 4 | 8 |
So far this is what I did in my query but it shows me duplicate record.
//get user groups store in database. example group in database:2, 3, 4
$GroupList = explode(", ", $UserDetail['u_group']);
foreach($GroupList as $a)
{
$getParentModuleSQL = base_executeSQL("SELECT * FROM parent_modules WHERE pmod_status<>'Disable'" );
$count1 = base_num_rows($getParentModuleSQL);
while($ParentModuledata_row = base_fetch_array($getParentModuleSQL))
if ($count1!= 0)
{
base_executeSQL("SELECT gp.gp_id AS ID FROM group_details AS gp, modules AS m WHERE m.pmod_id =". $ParentModuledata_row['pmod_id'] ." AND gp.gp_id=". $a ." AND gp.mod_id = m.mod_id" AND (base_gpd_rule_view='on' OR base_gpd_rule_add='on' OR base_gpd_rule_edit='on' OR base_gpd_rule_delete='on') GROUP BY gp.mod_id);
}
}
i still dont understand your problems. if you want all modules a gp_id has access to - select * from group_details where gp_id = XX. If you just want each module, select * from modules.