How to update multiple rows using single where clause - mysql

I have a table test and the structure is as shown below
mysql> select * from test;
+----+------------+------------+---------+-----+------+------+
| id | is_deleted | sort_order | version | cid | pid | qid |
+----+------------+------------+---------+-----+------+------+
| 14 | | 6 | 0 | 1 | 2 | 7 |
| 18 | | 1 | 2 | 1 | 4 | 12 |
| 26 | | 9 | 0 | 1 | 1 | 1 |
| 40 | | 1 | 0 | 1 | 5 | 18 |
| 49 | | 2 | 0 | 1 | 5 | 35 |
| 50 | | 3 | 0 | 1 | 5 | 39 |
| 51 | | 4 | 0 | 1 | 5 | 40 |
| 61 | | 10 | 0 | 1 | 1 | 2 |
| 62 | | 11 | 0 | 1 | 1 | 3 |
| 63 | | 12 | 0 | 1 | 1 | 4 |
| 64 | | 13 | 0 | 1 | 1 | 42 |
| 65 | | 14 | 0 | 1 | 1 | 43 |
| 66 | | 2 | 0 | 1 | 4 | 26 |
| 67 | | 3 | 0 | 1 | 4 | 38 |
| 71 | | 7 | 0 | 1 | 2 | 2 |
| 72 | | 8 | 0 | 1 | 2 | 8 |
| 73 | | 9 | 0 | 1 | 2 | 50 |
| 74 | | 10 | 0 | 1 | 2 | 51 |
| 76 | | 3 | 0 | 1 | 3 | 9 |
| 78 | | 4 | 0 | 1 | 3 | 53 |
| 79 | | 1 | 0 | 1 | 6 | 54 |
| 80 | | 1 | 0 | 1 | 11 | 59 |
| 81 | | 1 | 0 | 1 | 12 | 70 |
+----+------------+------------+---------+-----+------+------+
I want to set version to 1 of all the rows whose cid =1,pid not equal to 6,11 and qid not equal to 28,59,54
Can anybody please tell me how to do?

UPDATE test
SET `version` = 1
WHERE cid =1
AND pid NOT in(6,11)
AND qid NOT IN (28,59,54);

Related

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

How to compare a new value to last value in the table

I am trying to create a trigger in MySQL database. I have a table (myData) with 3 columns. Date, Values, and Status. What I am trying to achieve is, when a new value comes, if it is higher than the last value, It should insert 1 in the Status column. If it is less than the last value, It should insert 0 in the status column. I couldn't find a logic to do it. Any suggestions, please?
BEGIN
IF new.Values > // what should be here?
THEN
INSERT INTO //
END
Instead consider the following:
SELECT * FROM my_table;
+----+-------+
| id | value |
+----+-------+
| 1 | 2 |
| 2 | 12 |
| 3 | 13 |
| 4 | 9 |
| 5 | 7 |
| 6 | 6 |
| 7 | 8 |
| 8 | 3 |
| 9 | 10 |
| 10 | 1 |
| 11 | 18 |
| 12 | 4 |
| 13 | 6 |
| 14 | 0 |
| 15 | 2 |
| 16 | 8 |
| 17 | 14 |
| 18 | 7 |
| 19 | 15 |
| 20 | 11 |
| 21 | 12 |
| 22 | 7 |
| 23 | 20 |
| 24 | 17 |
| 25 | 8 |
| 26 | 6 |
| 27 | 6 |
| 28 | 12 |
| 29 | 3 |
| 30 | 18 |
| 31 | 1 |
| 32 | 12 |
+----+-------+
SELECT a.*
, b.value >= a.value n
FROM my_table a
LEFT
JOIN
( SELECT x.*
, MIN(y.id) next
FROM my_table x
LEFT
JOIN my_table y
ON y.id > x.id
GROUP
BY x.id
) b
ON b.next = a.id;
+----+-------+------+
| id | value | n |
+----+-------+------+
| 1 | 2 | NULL |
| 2 | 12 | 0 |
| 3 | 13 | 0 |
| 4 | 9 | 1 |
| 5 | 7 | 1 |
| 6 | 6 | 1 |
| 7 | 8 | 0 |
| 8 | 3 | 1 |
| 9 | 10 | 0 |
| 10 | 1 | 1 |
| 11 | 18 | 0 |
| 12 | 4 | 1 |
| 13 | 6 | 0 |
| 14 | 0 | 1 |
| 15 | 2 | 0 |
| 16 | 8 | 0 |
| 17 | 14 | 0 |
| 18 | 7 | 1 |
| 19 | 15 | 0 |
| 20 | 11 | 1 |
| 21 | 12 | 0 |
| 22 | 7 | 1 |
| 23 | 20 | 0 |
| 24 | 17 | 1 |
| 25 | 8 | 1 |
| 26 | 6 | 1 |
| 27 | 6 | 1 |
| 28 | 12 | 0 |
| 29 | 3 | 1 |
| 30 | 18 | 0 |
| 31 | 1 | 1 |
| 32 | 12 | 0 |
+----+-------+------+
If all you want is the rows where n=1, then the query is actually even simpler...
SELECT a.*
FROM my_table a
JOIN
( SELECT x.*
, MIN(y.id) next
FROM my_table x
LEFT
JOIN my_table y
ON y.id > x.id
GROUP
BY x.id
) b
ON b.next = a.id
AND b.value >= a.value;
+----+-------+
| id | value |
+----+-------+
| 4 | 9 |
| 5 | 7 |
| 6 | 6 |
| 8 | 3 |
| 10 | 1 |
| 12 | 4 |
| 14 | 0 |
| 18 | 7 |
| 20 | 11 |
| 22 | 7 |
| 24 | 17 |
| 25 | 8 |
| 26 | 6 |
| 27 | 6 |
| 29 | 3 |
| 31 | 1 |
+----+-------+

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.

Calculate sum, counts in many to many

Greetings I am trying to sum up expense value for each transaction.
Association table.
**Assoc table schema**
| PK_id | FK_transaction | FK_Expense |
|-------|----------------|------------|
| 1 | 1 | 85 |
| 2 | 2 | 81 |
| 3 | 3 | 77 |
| 4 | 4 | 83 |
| 5 | 5 | 84 |
| 6 | 6 | 105 |
| 7 | 7 | 104 |
| 8 | 8 | 71 |
| 9 | 8 | 88 |
| 10 | 8 | 90 |
Transaction table
**Transaction table schema**
| PK_id | type | value | confirmed_value |
|-------|------|-------|--------------------|
| 1 | 1 | 3.2 | 0 |
| 2 | 1 | 23.2 | 0 |
| 3 | 1 | 33.2 | 0 |
| 4 | 1 | 43.2 | 11.00 |
| 5 | 1 | 53.2 | 0 |
| 6 | 1 | 63.2 | 0 |
| 7 | 1 | 73.2 | 0 |
| 8 | 1 | 83.2 | 66.00 |
| 9 | 1 | 93.2 | 0 |
| 10 | 1 | 133.2 | 77.00 |
| 11 | 1 | 123.2 | 0 |
Expences Table
| PK_id | value |
|-------|-------|
| 85 | 3.2 |
| 81 | 23.2 |
| 77 | 33.2 |
| 83 | 43.2 |
| 84 | 53.2 |
| 105 | 63.2 |
| 104 | 73.2 |
| 71 | 83.2 |
| 88 | 93.2 |
| 90 | 133.2 |
Result ::
| PK_id | value | confirmed_value |
|-------|-------|-----------------|
| 1 | 3.2 | 0 |
| 2 | 23.2 | 0 |
| 3 | 33.2 | 0 |
| 4 | 43.2 | 11 |
| 5 | 53.2 | 0 |
| 6 | 63.2 | 0 |
| 7 | 73.2 | 0 |
| 8 | 83.2 | 66 |
| 8 | 93.2 | 66 |
| 8 | 133.2 | 66 |
Desired - before calculations ( just to give u the idea )
| PK_id | value | confirmed_value |
|-------|-------|-----------------|
| 1 | 3.2 | 0 |
| 2 | 23.2 | 0 |
| 3 | 33.2 | 0 |
| 4 | 43.2 | 11 |
| 5 | 53.2 | 0 |
| 6 | 63.2 | 0 |
| 7 | 73.2 | 0 |
| 8 | 309.6 | 66 |
Full desired result
count matching values COUNT(confirmed_value = value )
count entries that not match COUNT(confirmed_value != value )
/\ Calculate value of confirmed values
?? sum(transactions.confirmed_value)
and calculate value itself
Should be a row result ( example )
=======================================================================================================================
MATCHED | NOT_MATCHED | SUM(of values) | COUNT(of confirmed equal to value ) | COUNT(of confirmed ! equal to value )
=======================================================================================================================
1 | 10 | 3003.22 | 3 | 10
=======================================================================================================================
SQL Fiddle : http://sqlfiddle.com/#!9/2ab102/9
Thanks for any tips
Try this query -
SELECT transactions.PK_id,
SUM(expenses.value),
transactions.confirmed_value
FROM `assoc`
JOIN `expenses` ON `assoc`.`FK_Expense` = `expenses`.`PK_id`
JOIN `transactions` ON `assoc`.`FK_transaction` = `transactions`.`PK_id`
GROUP BY transactions.PK_id,
transactions.confirmed_value
Fiddle Demo - http://sqlfiddle.com/#!9/2ab102/14

Mysql rollup issue when grouping with quarter and date

I have this simple query:
select quarter(invitation_date) as `iQuarter`, invitation_date as `iDate`,count(*) as count from data group by iQuarter,iDate with rollup
I would expect to get a table like this:
+----------+------------+-------+
| iQuarter | iDate | count |
+----------+------------+-------+
| 1 | 2010-02-23 | 133 |
| 1 | 2010-03-02 | 14 |
| 1 | 2010-03-09 | 74 |
| 1 | 2010-03-15 | 102 |
| 1 | 2010-03-22 | 76 |
| 1 | 2010-03-30 | 16 |
| NULL | NULL | 415 |
| 2 | 2010-04-06 | 20 |
| 2 | 2010-04-12 | 84 |
| 2 | 2010-04-19 | 85 |
| 2 | 2010-04-26 | 51 |
| 2 | 2010-05-03 | 73 |
| 2 | 2010-05-11 | 76 |
| 2 | 2010-05-18 | 54 |
| 2 | 2010-05-24 | 83 |
| 2 | 2010-06-01 | 73 |
| 2 | 2010-06-07 | 74 |
| 2 | 2010-06-24 | 81 |
| 2 | 2010-06-28 | 110 |
| NULL | NULL | 864 |
+----------+------------+-------+
But what I get has the total in the wrong position and before every quarters total we have a row with the next quarter with a count of one.
+----------+------------+-------+
| iQuarter | iDate | count |
+----------+------------+-------+
| 1 | 2010-02-23 | 1 |
| NULL | NULL | 1 |
| 1 | 2010-02-23 | 132 |
| 1 | 2010-03-02 | 14 |
| 1 | 2010-03-09 | 74 |
| 1 | 2010-03-15 | 102 |
| 1 | 2010-03-22 | 76 |
| 1 | 2010-03-30 | 16 |
| 2 | 2010-04-06 | 1 |
| NULL | NULL | 415 |
| 2 | 2010-04-06 | 19 |
| 2 | 2010-04-12 | 84 |
| 2 | 2010-04-19 | 85 |
| 2 | 2010-04-26 | 51 |
| 2 | 2010-05-03 | 73 |
| 2 | 2010-05-11 | 76 |
| 2 | 2010-05-18 | 54 |
| 2 | 2010-05-24 | 83 |
| 2 | 2010-06-01 | 73 |
| 2 | 2010-06-07 | 74 |
| 2 | 2010-06-24 | 81 |
| 2 | 2010-06-28 | 110 |
| 3 | 2010-07-06 | 1 |
| NULL | NULL | 864 |
+----------+------------+-------+
Invitation date is defined as:
+-------------------+---------------------------+------+-----+---------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+---------------------------+------+-----+---------------------+-------+
| invitation_date | date | NO | | | |
+-------------------+---------------------------+------+-----+---------------------+-------+
Anyone have a clue on why this is happening? Mysql version: 5.0.27