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?
Related
I have two tables users and features:
users
+----+-----------------+
| id | email |
+----+-----------------+
| 1 | test1#gmail.com |
| 2 | test2#gmail.com |
| 3 | test3#gmail.com |
| 4 | test4#gmail.com |
| 5 | test5#gmail.com |
+----+-----------------+
features
+------------+---------+---------------------+------------+
| feature_id | user_id | feature_name | can_access |
+------------+---------+---------------------+------------+
| 1 | 1 | automated-investing | 1 |
| 2 | 1 | crypto | 0 |
| 3 | 2 | crypto | 0 |
| 4 | 3 | automated-investing | 0 |
| 5 | 4 | automated-investing | 1 |
| 7 | 1 | financial-tracking | 1 |
| 8 | 2 | financial-tracking | 0 |
| 9 | 3 | financial-tracking | 1 |
| 10 | 4 | financial-tracking | 0 |
+------------+---------+---------------------+------------+
I am trying to get the id from users based on email and then create a new row in features using that id ,feature_name and can_access(feature_name and can_access are from a request).
I am able to do it but it is split into two parts and I am looking to do it with a single query.
Current query:
// Get ID from email
SELECT id FROM users WHERE email="test5#gmail.com";
// Insert values into features
INSERT INTO features(user_id, feature_name, can_access) VALUES(5,"crypto", 1);
Expected output:
+------------+---------+---------------------+------------+
| feature_id | user_id | feature_name | can_access |
+------------+---------+---------------------+------------+
| 1 | 1 | automated-investing | 1 |
| 2 | 1 | crypto | 0 |
| 3 | 2 | crypto | 0 |
| 4 | 3 | automated-investing | 0 |
| 5 | 4 | automated-investing | 1 |
| 7 | 1 | financial-tracking | 1 |
| 8 | 2 | financial-tracking | 0 |
| 9 | 3 | financial-tracking | 1 |
| 10 | 4 | financial-tracking | 0 |
| 11 | 5 | crypto | 1 |
+------------+---------+---------------------+------------+
INSERT INTO features (user_id, feature_name, can_access)
SELECT id, "crypto", 1 FROM users WHERE email="test5#gmail.com";
So instead of specifying the values, you use the select statement.
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
I have a question very similar to this one, but different (aka, I couldn't extend the answer to that one to fit my purposes. Due to the second WHERE condition, specifically).
I have a table which tracks the visit number for customers. There are two types of visits:
| ID | InStoreVisit | InStoreDate | OnlineVisit | OnlineDate |
|----|--------------|-------------|-------------|------------|
| 1 | 1 | 1/1/11 | | |
| 1 | 2 | 1/2/11 | | |
| 1 | | | 1 | 1/3/11 |
| 1 | 3 | 1/4/11 | | |
| 2 | | | 1 | 2/2/12 |
| 2 | 1 | 2/3/12 | | |
| 2 | | | 2 | 2/4/12 |
I need to create a new column which has a sort of 'global visit number' as such:
| ID | InStoreVisit | InStoreDate | OnlineVisit | OnlineDate | GobalVisit |
|----|--------------|-------------|-------------|------------|------------|
| 1 | 1 | 1/1/11 | | | 1 |
| 1 | 2 | 1/2/11 | | | 2 |
| 1 | | | 1 | 1/3/11 | 3 |
| 1 | 3 | 1/4/11 | | | 4 |
| 2 | | | 1 | 2/2/12 | 1 |
| 2 | 1 | 2/3/12 | | | 2 |
| 2 | | | 2 | 2/4/12 | 3 |
I'm getting mixed up on the WHERE condition with which I can do the self-join. Any advice greatly appreciated.
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 |
+------------+------------+---------+
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 |
+-----+--------+------+----------+