how to store multiple id in multiple column - mysql

first table :
table Name : ssc_course;
+---------------+-----------------+------------+
| ssc_course_id | ssc_course_name | qualify_id |
+---------------+-----------------+------------+
| 1 | HSC | 1 |
| 2 | Diploma | 1 |
| 3 | ITI | 1 |
+---------------+-----------------+------------+
second table :
table name :ssc_stream
+---------------+--------------------+-----------------+
| ssc_stream_id | ssc_stream_name | ssc_course_id(fk) |
+---------------+--------------------+-----------------+
| 1 | Science | 1 |
| 2 | Commers | 1 |
| 3 | Arts | 1 |
| 17 | Computer Engg | 2 |
| 18 | Mechanical Engg | 2 |
| 19 | Civil Engg | 2 |
| 20 | Electronics & TELE | 2 |
| 21 | E&TC | 2 |
+---------------+--------------------+-----------------+
third table :(master table)
table name : ssc_college
+------------+--------------+---------------+---------------+-
| ssc_clg_id | clg_login_id | ssc_stream_id | ssc_course_id |
+------------+--------------+---------------+---------------+-
| 9 | 2 | 1 | 1 |
| 10 | 2 | 1 | 1 |
| 11 | 2 | 2 | 1 |
| 12 | 2 | 2 | 1 |
| 13 | 2 | 3 | 1 |
| 14 | 2 | 3 | 1 |
| 15 | 3 | 1 | 1 |
| 16 | 3 | 1 | 1 |
| 17 | 3 | 2 | 1 |
| 18 | 3 | 2 | 1 |
| 19 | 3 | 3 | 1 |
| 20 | 3 | 3 | 1 |
| 21 | 4 | 1 | 1 |
| 22 | 4 | 1 | 1 |
| 23 | 4 | 2 | 1 |
| 24 | 4 | 2 | 1 |
| 25 | 4 | 3 | 1 |
| 26 | 4 | 3 | 1 |
| 27 | 5 | 17 | 2 |
| 28 | 5 | 17 | 2 |
| 29 | 5 | 18 | 2 |
| 30 | 5 | 18 | 2 |
| 31 | 5 | 19 | 2 |
| 32 | 5 | 19 | 2 |
| 33 | 5 | 20 | 2 |
| 34 | 5 | 20 | 2 |
| 35 | 5 | 21 | 2 |
| 36 | 5 | 21 | 2 |
| 38 | 6 | 17 | 2 |
| 39 | 6 | 17 | 2 |
| 40 | 6 | 18 | 2 |
*************************************************************
I am trying to save course,stream id's into ssc_college table.
1 course have multiple stream how can i insert this type of values
ex. course 1-diploma and this course have multiple stream like-
1.comp. engg , 2.mechanical ,3.e&tc i want to add course id and
stream id at a time of third (ssc_college) table.
and multiple stream for multiple course how i can add at a time
multiple parent and multiple child id in mysql table.
Note: stream are dependant of course table.

Related

how can get single average row instead of multiple average rows?

I hava exam table in student database, i want show specific student with degree of each item in section 1,2 and avg section for each item
SELECT * FROM exam;
output:
id + numstudent + stage + item + exam +section + degree
---|------------|-------|------|------|--------|--------|
1 | stu_1991 | 2 | java | 1 | 1 | 70 |
--------------------------------------------------------|
2 | stu_1991 | 2 | java | 2 | 1 | 50 |
--------------------------------------------------------|
3 | stu_1991 | 2 | java | 1 | 2 | 60 |
--------------------------------------------------------|
4 | stu_1991 | 2 | java | 2 | 2 | 80 |
--------------------------------------------------------|
5 | stu_1991 | 2 | dart | 1 | 1 | 66 |
--------------------------------------------------------|
6 | stu_1991 | 2 | dart | 2 | 1 | 54 |
--------------------------------------------------------|
7 | stu_1991 | 2 | dart | 1 | 2 | 73 |
--------------------------------------------------------|
8 | stu_1991 | 2 | dart | 2 | 2 | 57 |
--------------------------------------------------------|
9 | stu_2222 | 1 | html | 1 | 1 | 55 |
--------------------------------------------------------|
10 | stu_2222 | 1 | html | 2 | 1 | 54 |
--------------------------------------------------------|
11 | stu_2222 | 1 | html | 1 | 2 | 73 |
--------------------------------------------------------|
12 | stu_2222 | 1 | html | 2 | 2 | 57 |
--------------------------------------------------------|
I want output like this:
id + numstudent + stage + item + exam +section + degree +avgsection+ avgfinal
---|------------|-------|------|------|--------|--------|----------|-----------|
1 | stu_1991 | 2 | java | 1 | 1 | 70 | | |
---------------------------------------------------------| | |
2 | stu_1991 | 2 | java | 2 | 1 | 50 | 60 | |
---------------------------------------------------------|----------| |
3 | stu_1991 | 2 | java | 1 | 2 | 60 | | |
---------------------------------------------------------| | |
4 | stu_1991 | 2 | java | 2 | 2 | 80 | 70 | 55 |
---------------------------------------------------------------------------------
5 | stu_1991 | 2 | dart | 1 | 1 | 66 | | |
---------------------------------------------------------| | |
6 | stu_1991 | 2 | dart | 2 | 1 | 54 | 60 | |
---------------------------------------------------------|----------| |
7 | stu_1991 | 2 | dart | 1 | 2 | 73 | | |
---------------------------------------------------------| | |
8 | stu_1991 | 2 | dart | 2 | 2 | 57 | 65 | 62.5 |

I want to retrieve which scheduler is scheduled for a particular day

+-----+--------------------------------------------------+--------+--------------------+---------------+---------------+---------------+--------------------------------------+-------------------------+-------------------+--------------------------------------------------------------------------------------------------------+--------------------------+--------+----------------+--------------------------------------+---------+
| ID | Name | Action | Created By Use Rid | Created Date | Days | Enterprise ID | Job ID | Last Updated By Use Rid | Last Updated Date | Relays | Sch Time | Status | Timezone | Trigger ID | User ID |
+-----+--------------------------------------------------+--------+--------------------+---------------+---------------+---------------+--------------------------------------+-------------------------+-------------------+--------------------------------------------------------------------------------------------------------+--------------------------+--------+----------------+--------------------------------------+---------+
| 30 | ios | 1 | 2 | 1559739565592 | | 2 | ef29baba-534d-4151-80d3-f5a589cf7c22 | 2 | 1559739565592 | 74 | 2019-06-05T13:04:00.000Z | 2 | Asia/Kolkata | 0492dc45-6671-4064-baef-8654761b67f2 | 2 |
| 31 | iot | 0 | 2 | 1559739617863 | 1,2,3,4,5,6,7 | 2 | 85e4a611-37dc-45d3-8a2f-243e6d4e0962 | 2 | 1575869484168 | 508,509,504,505,506,507 | 2019-12-09T13:50:00.000Z | 0 | Asia/Kolkata | b8a71ee4-94bd-4d2b-b875-7ac1468fad11 | 2 |
| 32 | Daily-8:50 Pm | 1 | 2 | 1559747777019 | | 2 | 3f87f532-6289-4016-a6d4-2a76baf4ffb5 | 2 | 1559749199040 | 45,46,47,48,50,51,52,61,62,63,64,65,302,303,73,74,75,76,77,53,54,55,56,57,58,59,60,49,66,67,68,304,305 | 2019-06-05T15:41:00.000Z | 2 | Asia/Kolkata | 6f1b873b-1e96-47bd-b723-4f50d5534f89 | 2 |
| 33 | TVOffSchedular | 0 | 2 | 1559933346322 | 1,2,3,4,5,6,7 | 4 | 1935f0cf-cdf9-4bdf-895d-9e6dc6c4ddff | 2 | 1560453392696 | 321,325 | 2019-06-13T19:20:00.000Z | 0 | Asia/Kolkata | c6bf383d-7bd7-4364-b077-e8bb3ad857f7 | 2 |
| 34 | TVOnMorning | 1 | 2 | 1559933392595 | 1,2,3,4,5,6,7 | 4 | d90d5c17-46b9-46cc-bb33-07557bf97684 | 2 | 1559933392595 | 321,325 | 1970-01-01T00:00:00.000Z | 2 | Asia/Kolkata | a131188d-aa7d-4132-995e-035e172566a7 | 2 |
| 35 | MorningOfficeON | 1 | 2 | 1560004379991 | 1,2,3,4,5,6 | 2 | 8b9163e7-bdea-4ee3-b827-3ab3d4728a0c | 2 | 1561881126773 | 49 | 2019-06-08T02:30:00.000Z | 0 | Asia/Kolkata | 926e809f-b1fb-4d3c-8da7-b048c0366a3a | 2 |
| 36 | EveningOfficeOFF | 0 | 2 | 1560005726195 | 1,2,3,4,5,6 | 2 | a7161d66-9ae4-4eb9-bc6e-ded6bd01a766 | 2 | 1560261033555 | 49 | 2019-06-11T14:00:00.000Z | 0 | Asia/Kolkata | dc45718f-aad4-4eb2-9b94-8a8bc4444385 | 2 |
| 37 | Good night | 1 | 3 | 1560088585521 | 1,2,3,4,5,6,7 | 3 | 4e8eedd1-956d-4922-9715-8f9b7bf221cb | 3 | 1560088585521 | 253,363 | 1970-01-01T14:00:00.000Z | 2 | Asia/Kolkata | 39a8fb76-06d6-4fdd-8c1f-827c3c1e2c24 | 3 |
| 38 | Good night off | 0 | 3 | 1560088719173 | 1,2,3,4,5,6,7 | 3 | c0a9dfdd-7b1b-4a9e-a9f1-b31d927d995e | 3 | 1560088719173 | 253,363 | 1969-12-31T19:30:00.000Z | 2 | Asia/Kolkata | 61cfacdd-f2c8-46c9-b192-228aef33cc59 | 3 |
| 39 | ApplicationON | 1 | 2 | 1560140790669 | 1,2,3,4,5,6,7 | 2 | 8f602cc4-8fe7-416a-b563-9b25cd008e5b | 2 | 1560141163056 | 45,46,47,48,50,51,52 | 2019-06-10T04:35:00.000Z | 0 | Asia/Kolkata | 5bab6ae0-7c7b-487c-9e72-d6200e0985a2 | 2 |
| 40 | ApplicationMorningOFF | 0 | 2 | 1560140849363 | 1,2,3,4,5,6,7 | 2 | 7d34c4f1-3638-4355-a14c-622bf733e23e | 2 | 1560141176600 | 45,46,47,48,50,51,52 | 2019-06-10T04:40:00.000Z | 0 | Asia/Kolkata | 4cd78892-c2b2-493e-a839-b270eff5aea5 | 2 |
| 41 | DesignNoonOFF | 0 | 2 | 1560241956756 | 1,2,3,4,5,6,7 | 2 | 00d3437b-a062-4f32-bb37-200574cac235 | 2 | 1567674559401 | 512,513,514,515 | 2019-09-05T08:15:00.000Z | 0 | Asia/Kolkata | b10d1ded-60a3-4adf-ba6c-743ec3dc4f29 | 2 |
| 42 | DesignNoonON | 1 | 2 | 1560241995428 | 1,2,3,4,5,6,7 | 2 | f3e3807c-7fad-4b08-bb18-439222f8d454 | 2 | 1567674587127 | 512,513,514,515 | 2019-09-05T08:30:00.000Z | 0 | Asia/Kolkata | c53e9069-164e-430a-89be-913c8127a767 | 2 |
| 43 | blockchain-test | 1 | 2 | 1560255684868 | 1,2,3,4,5,6,7 | 2 | 4ac65798-682e-447d-b6cf-813308f05efe | 2 | 1560257490068 | 45,46,47,48,50,51,52,61,62,63,64,65,302,73,74,75,76,53,54,57,58,59,60 | 2019-06-11T12:52:00.000Z | 2 | Asia/Calcutta | 8e7640ea-0223-4a9b-abdc-73c139f8e6fa | 2 |
| 44 | NightLampON | 1 | 2 | 1560360082007 | 1,2,3,4,5,6,7 | 4 | b5aee8d5-8013-4303-88df-6a5e3bc7330e | 2 | 1560360082007 | 322 | 1970-01-01T17:25:00.000Z | 0 | Asia/Kolkata | d0f3a1b5-93f3-4858-be5f-4837e62dd9bf | 2 |
| 45 | blockchain-off | 1 | 2 | 1560432958716 | 1,2,3,4,5,6,7 | 2 | a3d89bb2-3710-4f8e-9814-1e6942d1a405 | 2 | 1560432958716 | 53,54,57,59,60 | 2019-06-13T13:45:00.000Z | 0 | Asia/Calcutta | a0ccdcb6-a55e-48e2-9391-60d62e10fe4a | 2 |
| 46 | AllLightOffExceptOfficeMaster | 0 | 2 | 1560524588879 | | 2 | 64939311-522e-4877-b53f-e44ff78200b7 | 2 | 1560524588879 | 45,46,47,48,50,51,52,61,62,63,64,302,303,73,74,75,76,77,53,54,55,56,57,58,59,60 | 2019-06-14T15:10:00.000Z | 0 | Asia/Kolkata | 4a799d93-877c-4d19-b19a-40b6b9767564 | 2 |
| 47 | NightLampOFF | 0 | 2 | 1560533195543 | 1,2,3,4,5,6,7 | 4 | c844b235-3851-4692-ab14-4ee31b5d9404 | 2 | 1560620178500 | 322 | 2019-06-15T17:40:00.000Z | 0 | Asia/Kolkata | a48c0a99-76cd-4268-b946-8d63408aa234 | 2 |
| 48 | TVON | 1 | 2 | 1561124197968 | 1,2,3,4,5,6,7 | 4 | b0617f91-54b2-48bc-9cf9-2f42fb1526c4 | 2 | 1571035693157 | 321,325 | 2019-06-20T23:30:00.000Z | 0 | Asia/Kolkata | 93d07355-1c95-4faa-ac5c-73b0c68b5323 | 2 |
| 49 | panel light/Inside logo on | 1 | 3 | 1561492261954 | 1,2,3,4,5,6 | 3 | 736609ff-6391-474a-9556-85eff54f2fd8 | 3 | 1561492261954 | 359,360,361,364 | 2019-06-26T14:00:00.000Z | 2 | Asia/Kolkata | 84de41a7-a141-49d3-8bf2-8a9a229a8c56 | 3 |
| 50 | Outside Board/panel/focus light ON | 1 | 3 | 1562096267147 | 1,2,3,4,5,6,7 | 3 | 37d4f238-15b3-4a1a-bac1-a050d61792f7 | 3 | 1572442799792 | 894 | 1970-01-01T13:45:00.000Z | 2 | Asia/Kolkata | 3610c4b9-6180-4640-a078-3e310d80b4dd | 3 |
| 51 | Outside Board/Panel/Focus light OFF | 0 | 3 | 1562096428205 | 1,2,3,4,5,6,7 | 3 | 7ef0d450-39f9-4449-beec-c46416c3ebb2 | 3 | 1572442815806 | 894 | 2019-10-30T16:30:00.000Z | 2 | Asia/Kolkata | 8d9fe67d-7d39-4edb-8191-ea8af176b5d7 | 3 |
| 52 | Inside Logo/Panel/Display/Ganpati/Entry light ON | 1 | 3 | 1562096734383 | 1,2,3,4,5,6 | 3 | 4b6bab36-889a-45ca-969c-a1cecea1f42f | 3 | 1562096734383 | 110,111,359,360,361,364 | 2019-07-03T13:30:00.000Z | 2 | Asia/Kolkata | c08cc35a-8e64-4d17-a673-4bfddb2f1c92 | 3 |
| 53 | Inside Logo/Panel/Display/Ganpati/Entrance OFF | 0 | 3 | 1562097007454 | 1,2,3,4,5,6 | 3 | 54de42a4-f28e-494a-9ec3-56e22e7956e9 | 3 | 1562257875176 | 110,111,359,360,361,364 | 2019-07-03T16:30:00.000Z | 2 | Asia/Kolkata | ffbf0fcd-e6ae-41ba-8873-0d2bbd5d3cd0 | 3 |
| 54 | EveningOfficeON | 1 | 2 | 1562852792955 | | 2 | b4c3b59f-8157-4260-b0c0-a76b739e857b | 2 | 1562852792955 | 45,46,47,48,61,62,63,64,49 | 2019-07-11T14:01:00.000Z | 0 | Asia/Kolkata | 4be1a145-8e21-405e-990d-f85d0c3657bf | 2 |
| 55 | WaterCoolerON | 1 | 2 | 1564217226090 | 1,2,3,4,5,6,7 | 2 | 1e9c429f-9a51-4147-815b-efad95330baa | 2 | 1564217226090 | 489 | 1970-01-01T00:30:00.000Z | 0 | Asia/Kolkata | 4b9413ae-6d13-4117-bc8d-36f02c462b05 | 2 |
| 56 | WaterCoolerOff | 0 | 2 | 1564217269330 | 1,2,3,4,5,6,7 | 2 | 0f2c1112-dd26-43eb-8390-a1bba53789de | 2 | 1564217269330 | 489 | 1970-01-01T14:30:00.000Z | 0 | Asia/Kolkata | 907e4ee5-367a-4b4b-bc6b-716eeed4c8b8 | 2 |
| 57 | Test 1 | 1 | 28 | 1567236412976 | | 6 | 2c8276b3-a946-475c-a0e9-7e55569cadae | 28 | 1567236412976 | 553 | 2019-08-31T07:28:00.000Z | 2 | IST | dc1fec52-8c98-4832-b01a-a3a91f0b6058 | 28 |
| 58 | Test 3 | 1 | 28 | 1567236499938 | | 6 | fc820b65-a04c-4ecf-9876-2bd4bb8a8816 | 28 | 1567237094910 | 556,557 | 2019-08-31T07:39:00.000Z | 2 | IST | ceca92e5-765c-4f54-97ba-77447147276f | 28 |
| 59 | Test 1.1 | 1 | 28 | 1567236608884 | | 6 | 985e07c0-3a53-4884-a169-19e8a9ae630b | 28 | 1567236608884 | 553 | 2019-08-31T07:32:00.000Z | 2 | IST | f1a18b94-ef35-46a7-afbe-c75e743185fe | 28 |
| 60 | Outside C&C Ledlight On | 1 | 3 | 1570365557312 | 1,2,3,4,5,6,7 | 3 | ad17d61e-549e-44f1-99db-b50105d069b3 | 3 | 1574947645835 | 893 | 2019-11-28T12:30:00.000Z | 0 | Asia/Kolkata | 82b0fc02-c98c-4b12-9d8a-3ba2897d1aa7 | 3 |
| 61 | test | 0 | 2 | 1570532481972 | 1 | 2 | 2d058c32-89f8-4709-ba77-cca2c677334d | 2 | 1570532481972 | 524,525,526,527,529,530,531 | 2019-10-08T11:00:00.000Z | 2 | Africa/Lusaka | 722aece6-39d5-44be-bace-967180cd449f | 2 |
| 62 | Outside C&C Ledlight Off | 0 | 3 | 1570647085459 | 1,2,3,4,5,6,7 | 3 | 24c4831d-3491-4fa9-a8e2-dab21a94aa93 | 3 | 1572636959040 | 893 | 2019-10-29T19:00:00.000Z | 0 | Asia/Kolkata | 41c21970-faf8-4699-9a97-a49e1b28f3ce | 3 |
| 63 | bedroom allout 1 | 1 | 2 | 1571346220354 | 1,2,3,4,5,6,7 | 7 | 9ba4ec5b-c645-4cf6-bede-59fa79073134 | 2 | 1574015422525 | 662 | 1970-01-01T17:03:00.000Z | 0 | Asia/Calcutta | 4aff33ae-6af6-45f3-8407-f67a6f344e4b | 2 |
| 64 | bedroom allout off | 0 | 2 | 1571346263753 | 1,2,3,4,5,6,7 | 7 | 8cd3c6ac-0ea6-49b9-af09-10e9dcb3847b | 2 | 1571346263753 | 662 | 1970-01-01T01:04:00.000Z | 0 | Asia/Calcutta | 4ecfcf3f-110c-44b2-9a8d-3f35f1d5a295 | 2 |
| 65 | PIR Enable | 1 | 25 | 1571649294987 | 1,2,3,4,5,6,7 | 9 | c142b8f1-77d6-46a2-8f45-46ad3770a3c4 | 25 | 1571661763643 | 756 | 2019-10-21T13:00:00.000Z | 2 | Asia/Kolkata | 05fc4108-f65a-449f-a211-10e76e080196 | 25 |
| 66 | PIR Disable | 0 | 25 | 1571649351212 | 1,2,3,4,5,6,7 | 9 | 894ef781-cb8f-4139-907f-5d20fc5a0861 | 25 | 1571649351212 | 756 | 2019-10-21T01:30:00.000Z | 2 | Asia/Kolkata | 0f927aca-f592-44bb-9a7f-67544aa32223 | 25 |
| 67 | testing 22 | 1 | 2 | 1571734869588 | | 7 | 2fe74039-d25e-4384-bc4a-dd63843de063 | 2 | 1574015556036 | 639 | 2019-11-20T09:03:00.000Z | 0 | Asia/Calcutta | 86cfd858-367a-4a0b-b7eb-35e1096b56f4 | 2 |
| 68 | mobile charger | 0 | 23 | 1572199190164 | 1,2,3,4,5,6,7 | 11 | 7d536ef8-d24a-4a53-94c0-6c52a441312a | 23 | 1575485933026 | 773,774 | 2019-12-04T21:00:00.000Z | 0 | Asia/Calcutta | fc8b10b3-43a5-497c-86b9-bd55a80cfe4a | 23 |
| 69 | Outside Focus light On | 1 | 3 | 1572637078856 | 1,2,3,4,5,6,7 | 3 | 9a889394-8102-42a0-836a-43a7ffb5c7bf | 3 | 1572983697418 | 922 | 2019-11-06T12:30:00.000Z | 0 | Asia/Kolkata | 4747ca5e-736b-4b80-b7a4-9abfe33f0e77 | 3 |
| 70 | Outside Focus light Off | 0 | 3 | 1572637169312 | 1,2,3,4,5,6,7 | 3 | 1a283075-37e6-4152-a716-a8b63ef62f56 | 3 | 1572637169312 | 922 | 1969-12-31T18:35:00.000Z | 0 | Asia/Kolkata | fa79f7f6-bde4-4d87-96fd-b570ad47a57c | 3 |
| 71 | Ganpati/Display On | 1 | 3 | 1572637346879 | 2,3,4,5,6,7 | 3 | 6ae8c9ca-e26b-48b8-a3be-ef2d41b0a0d6 | 3 | 1574943671391 | 924,926,894 | 2019-11-28T12:30:00.000Z | 0 | Asia/Kolkata | 0027b2eb-efa2-41ff-ac24-50483eec4632 | 3 |
| 72 | Ganpati/Display Off | 0 | 3 | 1572637442853 | 2,3,4,5,6,7 | 3 | 0bb73aca-c82d-4459-922b-cb3592783ae9 | 3 | 1574943713949 | 924,926,894 | 2019-11-28T16:30:00.000Z | 0 | Asia/Kolkata | f8fb596e-dd42-4227-98cb-03a8dcbee9bb | 3 |
| 73 | panel/Logo/Table logo/ Light On | 1 | 3 | 1572638062814 | 2,3,4,5,6,7 | 3 | ab78a500-af55-4c8c-84a8-fdbbe76404f4 | 3 | 1572638089623 | 853,855 | 2019-11-02T12:30:00.000Z | 0 | Asia/Kolkata | 0ef71b02-744c-4755-b7dc-bc51dd727fe9 | 3 |
| 74 | all-out-off | 0 | 13 | 1572887848318 | 2,3,4,5,6 | 10 | 213f3bab-332a-4992-9d2d-7e38cf48dbfd | 13 | 1572887848318 | 838 | 2019-11-04T01:30:00.000Z | 0 | Asia/Calcutta | 70938fed-0053-443e-b302-e0f8e25826bb | 13 |
| 75 | mbroom-fan-off | 0 | 13 | 1572888838651 | 1,2,3,4,5,6,7 | 10 | ae1b74c2-2ad7-4182-9b4d-3f15812edbe8 | 13 | 1572974583661 | 834 | 2019-11-04T20:45:57.000Z | 0 | Asia/Calcutta | e87e19e4-d8ae-45b4-b575-1110d1368648 | 13 |
| 76 | PIR Enable | 1 | 25 | 1572944519175 | 1,2,3,4,5,6,7 | 9 | 6fc17694-4fc5-443e-92ee-ed1132a2ec35 | 25 | 1572944519175 | 758 | 2019-11-05T12:46:00.000Z | 0 | Asia/Kolkata | 46ea68cd-7e11-4003-bdfb-54aab6ba3923 | 25 |
| 77 | PIR Disable | 0 | 25 | 1572944690715 | 1,2,3,4,5,6,7 | 9 | ae7a333c-ff75-4c8b-bcae-92b308180b93 | 25 | 1573197064859 | 758 | 2019-11-06T00:20:00.000Z | 0 | Asia/Kolkata | 6a41e2f1-d029-4d28-9ce7-b2adebb59dab | 25 |
| 78 | test 1 | 1 | 2 | 1573129815561 | | 16 | ab60b929-48ed-43ab-9a89-45bfc0a10ee3 | 2 | 1573129815561 | 1030 | 2019-11-07T12:32:13.000Z | 0 | Asia/Kolkata | 80c7d1b2-68b4-425e-91b4-d82544d66dfc | 2 |
| 79 | pir en test | 1 | 25 | 1573196361376 | 1,2,3,4,5,6,7 | 9 | a206b1f6-f1e8-4c12-a97f-b7d217fd344a | 25 | 1573196739778 | 758 | 2019-11-08T07:08:00.000Z | 2 | Asia/Kolkata | dc23b8ee-2da0-42a3-a6d0-35a328035e12 | 25 |
| 80 | pir dbtest | 0 | 25 | 1573196399643 | 1,2,3,4,5,6,7 | 9 | 5d133d17-8495-4e34-aa6c-ea13344ddfdc | 25 | 1573196765147 | 758 | 2019-11-08T07:09:00.000Z | 2 | Asia/Kolkata | b431f3fc-f541-4912-b698-83f574c1c811 | 25 |
| 81 | living-off | 0 | 13 | 1573408027950 | | 10 | cac70a87-98b3-405f-a6da-2ac6f1c3a344 | 13 | 1573408137563 | 813 | 2019-11-10T20:30:56.000Z | 0 | Asia/Calcutta | 7b91172a-da7b-4f94-bf92-7915b51fce98 | 13 |
| 82 | ro on | 1 | 2 | 1573416862256 | 1,2,3,4,5,6,7 | 7 | 22e7e1ab-d2f3-483e-bcc5-3aa595feedb8 | 2 | 1573416862256 | 672 | 1970-01-01T00:08:00.000Z | 0 | Asia/Calcutta | 747f6396-77b7-4bab-93a2-6c6715b3e87f | 2 |
| 83 | RO off | 0 | 2 | 1573473725301 | 1,2,3,4,5,6,7 | 7 | eed1acbd-0850-4dc1-ad65-2528eb630389 | 2 | 1573473725301 | 672 | 1969-12-31T19:01:00.000Z | 0 | Asia/Calcutta | 89a511e2-52f6-45f3-9257-78b7f9f41bc2 | 2 |
| 84 | Test | 1 | 42 | 1573738374195 | | 17 | fca106ba-af83-4bae-8855-aa4d9edd4484 | 42 | 1573738590203 | 1133,1134,1135,1136 | 2019-11-14T13:38:29.000Z | 0 | Asia/Kolkata | f668dc34-db5b-4f34-a5a1-ee7649bc093d | 42 |
| 85 | entrance | 0 | 42 | 1573738946510 | | 17 | ce332a00-b3ca-45cc-9c1d-c3ead60845fd | 42 | 1573738946510 | 1133 | 2019-11-14T13:47:25.000Z | 0 | Asia/Kolkata | c7b912db-096a-4836-8b1d-0b52197922c5 | 42 |
| 86 | ronak bedroom charger | 1 | 15 | 1573827203905 | 1,2,3,4,5,6,7 | 15 | a31cc0a3-cfcd-4a57-8a72-f292893c4de9 | 15 | 1574274253149 | 1103 | 2019-11-14T23:30:00.000Z | 0 | Asia/Kolkata | 91a3e0c7-f750-42ca-84e0-af4bbb7ef839 | 15 |
| 87 | ronak bedroom plug off | 0 | 15 | 1573827259380 | 1,2,3,4,5,6,7 | 15 | 7f9e1f0f-e126-4bdd-acce-967bfb785c25 | 15 | 1574274268343 | 1103 | 2019-11-15T01:30:00.000Z | 0 | Asia/Kolkata | dc3b7742-711c-46c5-9395-38c9568d32dc | 15 |
| 88 | fan off | 0 | 15 | 1573845123332 | 1,2,3,4,5,6,7 | 15 | d5c68535-499a-436d-844f-8d0d7140d2da | 15 | 1573845123332 | 1096 | 2019-11-15T20:32:00.000Z | 0 | Asia/Kolkata | a014d04c-48dc-4ec4-a56f-90383387c241 | 15 |
| 89 | master beroom all out on | 1 | 20 | 1574014827858 | 1,2,3,4,5,6,7 | 8 | f25ae2d2-9414-418a-878b-1545a956d5fe | 20 | 1574014827858 | 700 | 2019-11-17T17:00:00.000Z | 0 | Asia/Kolkata | 3cded4f6-9740-43fa-b921-b5b9deb5bdb5 | 20 |
| 90 | master bedroom plug 4 | 0 | 20 | 1574015013470 | 1,2,3,4,5,6,7 | 8 | 1daabfe0-ccaa-4821-8658-c556541771a4 | 20 | 1574042395372 | 700 | 2019-11-18T00:30:00.000Z | 0 | Asia/Kolkata | fa0aad18-fe38-4e2f-8753-caef702ab74c | 20 |
| 91 | light on | 0 | 22 |
+-----+--------------------------------------------------+--------+--------------------+---------------+---------------+---------------+--------------------------------------+-------------------------+-------------------+--------------------------------------------------------------------------------------------------------+--------------------------+--------+----------------+--------------------------------------+---------+
In the below data the sch time needs to be compared with Days ie when the Days column is null then sch Time should be the output and when it is 1,2,3,4,5,6,7 then case when 1 then return sunday, case when 2 then return monday,case when 3 then return tues, case when 4 then return wed, case when 5 then return thur, case when 6 then return fri, case when 7 then return sat. should be the output The data should be time and date sorted decreasing
Please check the below query and put your table name at the place Your_table.
ALTER TABLE dbo.dates ADD update_stat BIT DEFAULT(0) --You can add this in current table or temp table
WHILE EXISTS (
SELECT TOP 1 update_stat
FROM Your_Table
WHERE update_stat = 0
ORDER BY Id
)
BEGIN
DECLARE #days VARCHAR(100)
DECLARE #datepart VARCHAR(10)
DECLARE #Id INT
SET #datepart = DATEPART(dw, getdate())
SET #days = (
SELECT TOP 1 days
FROM dbo.dates
WHERE days IS NOT NULL
ORDER BY ID
)
SET #ID = (
SELECT TOP 1 ID
FROM dbo.dates
WHERE days IS NOT NULL
ORDER BY ID
)
IF CHARINDEX(#datepart, #days) > 0
BEGIN
UPDATE Your_Table
SET update_stat = 1
WHERE id = #Id
END
SELECT
SELECT DATEPART(d, getdate()) AS DaysNo
,CASE DATEPART(WEEKDAY, GETDATE())
WHEN 1
THEN 'SUNDAY'
WHEN 2
THEN 'MONDAY'
WHEN 3
THEN 'TUESDAY'
WHEN 4
THEN 'WEDNESDAY'
WHEN 5
THEN 'THURSDAY'
WHEN 6
THEN 'FRIDAY'
WHEN 7
THEN 'SATURDAY'
END AS DaysName
,*
FROM Your_Table
WHERE update_stat = 1
You can insert the new column the update_stat either in the same table or you can insert all the existing table data into temp table and then add a update_stat column using below query and rest are same start from WHILE Loop.
SELECT * INTO ##tempTable
FROM Your_Table
ALTER TABLE dbo.dates ADD update_stat BIT DEFAULT(0)---rest are same..

Is there a way to grab associated data from three different tables and display them?

I have four tables, three of which I need data from, as well as a fourth that stores how this data is associated. When running my query, I'm getting the wrong output and it seems like I'm not linking things up correctly, but I'm unsure as to how to do so. How do I link my joins to get the output I'm looking for?
So far, I've used
SELECT c.class, p.name, s.specialization
FROM players_classes pc
JOIN players p ON p.player_id=pc.player_id
JOIN classes c ON c.class_id=pc.class_id
JOIN specialization s ON s.spec_id=pc.spec_id
which references
CREATE TABLE players(
player_id INT UNSIGNED auto_increment PRIMARY KEY,
name VARCHAR(20)
)...;
CREATE TABLE classes(
class_id INT UNSIGNED auto_increment PRIMARY KEY,
class VARCHAR(20)
)...;
CREATE TABLE specializations(
spec_id INT UNSIGNED auto_increment PRIMARY KEY,
class_id INT UNSIGNED,
specialization VARCHAR(20)
)...;
but I'd like to be using this table in some way to display the information correctly linked, I'm just unsure how to do so:
CREATE TABLE players_classes(
pc_id INT UNSIGNED auto_increment PRIMARY KEY,
FOREIGN KEY(class_id) REFERENCES classes(class_id),
FOREIGN KEY(player_id) REFERENCES players(player_id),
FOREIGN KEY(spec_id) REFERENCES specializations(spec_id)
)...;
I'm expecting to be able to grab a player's name as well as their associated class and specialization, actual results are showing the values associated with 1/1/1 for each id, and so on and so forth.
edit: the data from players_classes is as follows
pc_id | class_id | player_id | spec_id |
1 3 1 8
2 12 2 35
3 2 3 6
etc.
therefore the expected result from this is
class | name | spec
paladin seranul holypa
hunter contherious marksmanship
priest unicorns holypr
I am instead getting
class | name | spec
warlock Affliction Affliction
warlock Grireaver Demonology
warlock Affliction Demonology
and so on throughout the table, listing combinations that do not exist within my players_classes table
specializations table
+---------+----------+----------------+
| spec_id | class_id | specialization |
+---------+----------+----------------+
| 1 | 1 | Affliction |
| 2 | 1 | Destruction |
| 3 | 1 | Demonology |
| 4 | 2 | Shadow |
| 5 | 2 | Discipline |
| 6 | 2 | HolyPr |
| 7 | 3 | Retribution |
| 8 | 3 | HolyPa |
| 9 | 3 | ProtectionPa |
| 10 | 4 | ProtectionWa |
| 11 | 4 | Arms |
| 12 | 4 | Fury |
| 13 | 5 | FrostMa |
| 14 | 5 | Fire |
| 15 | 5 | Arcane |
| 16 | 6 | vengeance |
| 17 | 6 | havoc |
| 18 | 7 | guardian |
| 19 | 7 | balance |
| 20 | 7 | feral |
| 21 | 7 | restorationDr |
| 22 | 8 | elemental |
| 23 | 8 | enhance |
| 24 | 8 | restorationSh |
| 25 | 9 | frostDk |
| 26 | 9 | blood |
| 27 | 9 | unholy |
| 28 | 10 | outlaw |
| 29 | 10 | assassin |
| 30 | 10 | subtlety |
| 31 | 11 | brewmaster |
| 32 | 11 | windwalker |
| 33 | 11 | mistweaver |
| 34 | 12 | BeastMaster |
| 35 | 12 | marksmanship |
| 36 | 12 | Survival |
+---------+----------+----------------+
classes table
+----------+-------------+
| class_id | class |
+----------+-------------+
| 1 | warlock |
| 2 | priest |
| 3 | paladin |
| 4 | warrior |
| 5 | mage |
| 6 | demonhunter |
| 7 | druid |
| 8 | shaman |
| 9 | deathknight |
| 10 | rogue |
| 11 | monk |
| 12 | hunter |
+----------+-------------+
players table
+-----------+--------------+
| player_id | name |
+-----------+--------------+
| 1 | Seranul |
| 2 | Contherious |
| 3 | Unicorns |
| 4 | Remereili |
| 5 | Affliction |
| 6 | Meowing |
| 7 | Brobot |
| 8 | Bagelsbbq |
| 9 | Rafusen |
| 10 | Taiboku |
| 11 | Yikes |
| 12 | Thunderblaze |
| 13 | Muo |
| 14 | Intz |
| 15 | Trunks |
| 16 | Kalphyte |
| 17 | Eyeoftheshoe |
| 18 | Amuhnet |
| 19 | Synkka |
| 20 | Affliction |
| 21 | Kts |
| 22 | Shadowdreams |
| 23 | Zahel |
| 24 | Azrama |
| 25 | Seranul |
| 26 | Momspaghetti |
| 27 | Ohki |
| 28 | Rafusen |
| 29 | Cindyy |
| 30 | Grireaver |
| 31 | Intz |
| 32 | lazy |
| 33 | missworld |
| 34 | Affliction |
| 35 | Amuhnet |
| 36 | eyeoftheshoe |
| 37 | sanctus |
| 38 | nozshelen |
| 39 | Contherious |
| 40 | messer |
| 41 | catathor |
| 42 | demonblaze |
| 43 | wrillett |
| 44 | raagnnar |
| 45 | xizi |
| 46 | nemesix |
| 47 | zeroskill |
| 48 | chikfillidan |
| 49 | tentenlol |
| 50 | unicorns |
| 51 | bubuhtide |
| 52 | ohki |
| 53 | azrama |
+-----------+--------------+
players_classes table
+-------+----------+-----------+---------+
| pc_id | class_id | player_id | spec_id |
+-------+----------+-----------+---------+
| 1 | 3 | 1 | 8 |
| 2 | 12 | 2 | 35 |
| 3 | 2 | 3 | 6 |
| 4 | 11 | 4 | 31 |
| 5 | 1 | 5 | 1 |
| 6 | 12 | 6 | 34 |
| 7 | 2 | 7 | 6 |
| 8 | 11 | 8 | 31 |
| 9 | 2 | 9 | 6 |
| 10 | 7 | 10 | 21 |
| 11 | 4 | 11 | 11 |
| 12 | 8 | 12 | 22 |
| 13 | 4 | 13 | 12 |
| 14 | 5 | 14 | 13 |
| 15 | 3 | 15 | 7 |
| 16 | 11 | 16 | 33 |
| 17 | 8 | 17 | 22 |
| 18 | 2 | 18 | 6 |
| 19 | 8 | 19 | 23 |
| 20 | 11 | 20 | 33 |
| 21 | 5 | 21 | 13 |
| 22 | 6 | 22 | 17 |
| 23 | 10 | 23 | 29 |
| 24 | 8 | 24 | 22 |
| 25 | 11 | 25 | 31 |
| 26 | 11 | 26 | 32 |
| 27 | 4 | 27 | 11 |
| 28 | 5 | 28 | 13 |
| 29 | 7 | 29 | 19 |
| 30 | 1 | 30 | 3 |
| 31 | 9 | 31 | 25 |
| 32 | 3 | 32 | 8 |
| 33 | 9 | 33 | 25 |
| 34 | 1 | 34 | 3 |
| 35 | 2 | 35 | 6 |
| 36 | 4 | 36 | 11 |
| 37 | 5 | 37 | 13 |
| 38 | 5 | 38 | 13 |
| 39 | 9 | 39 | 25 |
| 40 | 6 | 40 | 17 |
| 41 | 10 | 41 | 28 |
| 42 | 2 | 42 | 6 |
| 43 | 8 | 43 | 24 |
+-------+----------+-----------+---------+
You need to use the relationship table in your query:
SELECT c.class, p.name, s.specialization
FROM players_classes pc
JOIN players p ON p.player_id = pc.player_id
JOIN classes c ON c.class_id = pc.class_id
JOIN specializations s ON s.spec_id = pc.spec_id
DEMO
You shouldn't even have class_id and spec_id in the other tables, since these are many-to-many relationships, and those columns can only be used for one-to-one relationships.

Group by functioning in sql [duplicate]

This question already has answers here:
MySQL - Selecting a Column not in Group By
(4 answers)
SQL select only rows with max value on a column [duplicate]
(27 answers)
Closed 5 years ago.
I am running below queries on my table.
Table:
+----+-------------+--------------+----------------------------+------------+--------+
| id | Qty_holding | Qty_reserved | created | tokenid_id | uid_id |
+----+-------------+--------------+----------------------------+------------+--------+
| 1 | 10 | 0 | 2018-01-18 10:52:14.957027 | 1 | 1 |
| 2 | 20 | 0 | 2018-01-18 11:20:08.205006 | 8 | 1 |
| 3 | 110 | 0 | 2018-01-18 11:20:21.496318 | 14 | 1 |
| 4 | 10 | 0 | 2018-01-23 14:26:49.124607 | 1 | 2 |
| 5 | 3 | 0 | 2018-01-23 15:00:26.876623 | 11 | 2 |
| 6 | 7 | 0 | 2018-01-23 15:08:41.887240 | 11 | 2 |
| 7 | 11 | 0 | 2018-01-23 15:22:48.424224 | 11 | 2 |
| 8 | 15 | 0 | 2018-01-23 15:24:03.419907 | 11 | 2 |
| 9 | 19 | 0 | 2018-01-23 15:24:26.531141 | 11 | 2 |
| 10 | 23 | 0 | 2018-01-23 15:27:11.549538 | 11 | 2 |
| 11 | 27 | 0 | 2018-01-23 15:27:24.162944 | 11 | 2 |
| 12 | 7.7909428 | 0.11459088 | 2018-01-23 15:27:24.168643 | 1 | 2 |
| 13 | 3 | 0 | 2018-01-23 15:36:51.412340 | 14 | 2 |
| 14 | 7.5585988 | 0.11459088 | 2018-01-23 15:36:51.417177 | 1 | 2 |
| 15 | 6 | 0 | 2018-01-24 08:43:46.635069 | 14 | 2 |
| 16 | 7.3262548 | 0.11459088 | 2018-01-24 08:43:46.639984 | 1 | 2 |
| 17 | 9 | 0 | 2018-01-24 10:09:08.207816 | 14 | 2 |
| 18 | 7.0939108 | 0.11459088 | 2018-01-24 10:09:08.212842 | 1 | 2 |
| 19 | 6 | 3 | 2018-01-24 13:43:08.929586 | 14 | 2 |
| 20 | 3 | 6 | 2018-01-24 14:49:56.960112 | 14 | 2 |
| 21 | 0 | 9 | 2018-01-24 14:50:33.423671 | 14 | 2 |
| 22 | 30 | 9 | 2018-01-24 14:51:14.865453 | 14 | 2 |
| 23 | 4.7704708 | 0.11459088 | 2018-01-24 14:51:14.870256 | 1 | 2 |
| 24 | 27 | 12 | 2018-01-24 14:56:56.914009 | 14 | 2 |
| 25 | 24 | 15 | 2018-01-24 14:57:56.475939 | 14 | 2 |
| 26 | 21 | 15 | 2018-01-24 14:58:06.750903 | 14 | 2 |
| 27 | 18 | 15 | 2018-01-24 15:02:43.203878 | 14 | 2 |
| 28 | 4.7705074 | 0.11459088 | 2018-01-24 15:02:43.224901 | 1 | 2 |
| 29 | 24 | 0 | 2018-01-24 15:03:40.421943 | 11 | 2 |
| 30 | 4.9535074 | 0.11459088 | 2018-01-24 15:03:40.441552 | 1 | 2 |
| 31 | 1 | 0 | 2018-01-26 10:35:33.173801 | 18 | 2 |
| 32 | 10 | 15 | 2018-01-26 12:46:03.780807 | 14 | 2 |
+----+-------------+--------------+----------------------------+------------+--------+
Query 1:
select uid_id
, tokenid_id
, max(created) as max_created
from accounts_userholding
group
by uid_id
, tokenid_id
+--------+------------+----------------------------+
| uid_id | tokenid_id | max_created |
+--------+------------+----------------------------+
| 1 | 1 | 2018-01-18 10:52:14.957027 |
| 1 | 8 | 2018-01-18 11:20:08.205006 |
| 1 | 14 | 2018-01-18 11:20:21.496318 |
| 2 | 1 | 2018-01-24 15:03:40.441552 |
| 2 | 11 | 2018-01-24 15:03:40.421943 |
| 2 | 14 | 2018-01-26 12:46:03.780807 |
| 2 | 18 | 2018-01-26 10:35:33.173801 |
+--------+------------+----------------------------+
Query 2:
select uid_id
, Qty_holding
, Qty_reserved tokenid_id
, max(created) as max_created
from accounts_userholding
group
by uid_id
, tokenid_id
+--------+-------------+--------------+------------+----------------------------+
| uid_id | Qty_holding | Qty_reserved | tokenid_id | max_created |
+--------+-------------+--------------+------------+----------------------------+
| 1 | 10 | 0 | 1 | 2018-01-18 10:52:14.957027 |
| 1 | 20 | 0 | 8 | 2018-01-18 11:20:08.205006 |
| 1 | 110 | 0 | 14 | 2018-01-18 11:20:21.496318 |
| 2 | 10 | 0 | 1 | 2018-01-24 15:03:40.441552 |
| 2 | 3 | 0 | 11 | 2018-01-24 15:03:40.421943 |
| 2 | 3 | 0 | 14 | 2018-01-26 12:46:03.780807 |
| 2 | 1 | 0 | 18 | 2018-01-26 10:35:33.173801 |
+--------+-------------+--------------+------------+----------------------------+
The Qty_holding value in above is not corresponding to latest date. For instance for tokenid_id 14 and uid_id as 2 latest record is
| 32 | 10 | 15 | 2018-01-26 12:46:03.780807 | 14 | 2 |
But above query is giving qty_holding as 3.
Any insights in functioning of mysql will be helpful . Thanks!
As a rule of thumb: When you mix normal columns with aggregate functions in SELECT, you need to use GROUP BY. Do not use GROUP BY when you do not have normal columns and aggregate functions in SELECT.
The thing to put into the GROUP BY, is all from SELECT but the aggregate functions (and possible constants).
As an example if you have a query:
select a, substring(b,3), 'x', max(y)
from yourtable
You need to use GROUP BY. You leave out 'x' as it is a constant and you leave out the aggregate function. The rest goes to the GROUP BY.
select a, substring(b,3), 'x', max(y)
from yourtable
group by a, substring(b,3)
Previous MySQL versions allowed quite liberal use of GROUP BY resulting quite often just bad/incorrect code.

How to delete some specify row from a table

I have a test table
mysql> select * from test1 limit 20;
+------+------+----------------------------------+
| id | num | pass |
+------+------+----------------------------------+
| 1 | 1 | c4ca4238a0b923820dcc509a6f75849b |
| 2 | 3 | c81e728d9d4c2f636f067f89cc14862c |
| 1 | 1 | c4ca4238a0b923820dcc509a6f75849b |
| 2 | 2 | c81e728d9d4c2f636f067f89cc14862c |
| 1 | 1 | c4ca4238a0b923820dcc509a6f75849b |
| 2 | 3 | c81e728d9d4c2f636f067f89cc14862c |
| 3 | 3 | eccbc87e4b5ce2fe28308fd9f2a7baf3 |
| 4 | 6 | a87ff679a2f3e71d9181a67b7542122c |
| 5 | 7 | e4da3b7fbbce2345d7772b0674a318d5 |
| 6 | 6 | 1679091c5a880faf6fb5e6087eb1b2dc |
| 7 | 8 | 8f14e45fceea167a5a36dedd4bea2543 |
| 8 | 11 | c9f0f895fb98ab9159f51fd0297e236d |
| 9 | 10 | 45c48cce2e2d7fbdea1afc51c7c6ad26 |
| 10 | 12 | d3d9446802a44259755d38e6d163e820 |
| 11 | 12 | 6512bd43d9caa6e02c990b0a82652dca |
| 12 | 18 | c20ad4d76fe97759aa27a0c99bff6710 |
...
I want to delete the 1st,3rd and 9th row.So I use such command
delete from test1 limit 1,3,9;
But it doesn't work,is there any workaround can do this?