How to match comma delimited value with other column in mySql - 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 |
+-----+--------+------+----------+

Related

in a m to n table between PKs of table A and B. how to find a PK of A that has a specific set of entries with PKs of B in the m to n table

Given the two tables teacher and student,
table : teacher
+-------+-----------+
| id | name |
+-------+-----------+
| 1 | John |
| 2 | Mary |
| 3 | Jeff |
| 4 | Bill |
| 5 | Bob |
| 6 | Frieda |
+-------+-----------+
table : student
+-------+-----------+
| id | name |
+-------+-----------+
| 1 | mario |
| 2 | lisa |
| 3 | anna |
| 4 | sara |
| 5 | felix |
+-------+-----------+
and the m to n relationship between them
table : teacherStudent
+---------+---------+
|teacherId|studentId|
+---------+---------+
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 3 | 1 |
| 3 | 5 |
| 4 | 4 |
| 4 | 5 |
| 5 | 1 |
| 5 | 2 |
| 5 | 3 |
| 6 | 1 |
| 6 | 2 |
| 6 | 3 |
| 6 | 4 |
+---------+---------+
how to find all teacherId that stand in relation to (exactly) studentId 1,2 and 3 ?
I want my final results to look like this:
+----------+
|teacherId |
+----------+
| 1 |
| 5 |
+----------+

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.

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 |
+------------+------------+---------+

select data from one table based on the data status on other table

My first data table is couponsnmaster
+-----------+----------+------------+
|couponsnid | couponid | couponsn |
+-----------+----------+------------+
| 1 | 1 | 1000 |
| 2 | 1 | 1001 |
| 3 | 1 | 1002 |
| 4 | 1 | 1003 |
| 5 | 1 | 1004 |
| 6 | 1 | 1005 |
+-----------+----------+------------+
My second data table is distribute
+-----------+--------------+--------------+--------------+
| distid | couponid | couponsnid | status |
+-----------+--------------+--------------+--------------+
| 1 | 1 | 1 | distribute |
| 2 | 1 | 2 | distribute |
| 3 | 1 | 3 | distribute |
| 4 | 1 | 1 | returned |
+-----------+--------------+--------------+--------------+
I want to fetch all "couponsn" from "couponsnmaster" with respect to "couponid" except status is " distribute" or "sold" or "bonus" in table "distribute"....
Try this query:
SELECT c.couponsn FROM
couponsmaster c INNER JOIN distribute d
ON c.couponsid = d.couponsid
WHERE d.status NOT IN('distribute','sold','bonus')