I'm writing a query to update some chained rowsthat looks like this
SELECT T.Id,
T.Id + 1 AS NewID,
T.DifferenceTime,
RAT.AllowedDate,
RAT.ExpirationDate,
DATE_SUB(RAT.AllowedDate, INTERVAL T.DifferenceTime MINUTE) AS NewAllowedDate,
DATE_SUB(RAT.ExpirationDate, INTERVAL T.DifferenceTime MINUTE) AS NewExpiringDate
FROM ROOMS_ACCESS_TOKENS RAT
INNER JOIN (
SELECT T2.*,
ABS(TIMESTAMPDIFF(MINUTE,T2.ExpirationDate,UTC_TIMESTAMP())) AS DifferenceTime
FROM ROOMS_ACCESS_TOKENS RAT
INNER JOIN (
SELECT *
FROM ROOMS_ACCESS_TOKENS
WHERE Expired = 0 AND ExpirationDate > UTC_TIMESTAMP()
) AS T2 ON T2.IdRoom = RAT.IdRoom AND T2.ChainId = RAT.ChainId
WHERE RAT.IdRoom = 1
AND RAT.AccessToken = '5092FFA0B3721AE5EC757649DE983F8020220104215810'
AND RAT.RefId = 'B49F723F2FE0FD38063DA26F3EAEDBED'
AND RAT.KeyCode = '2773217uysagkasd8wqejhjkasdsm'
AND RAT.ExpirationDate > UTC_TIMESTAMP()
AND RAT.Expired = 0
) AS T ON T.IdRoom = RAT.IdRoom
AND T.ChainId = RAT.ChainId
AND T.AccessToken = RAT.AccessToken
AND T.RefId = RAT.RefId
AND T.AllowedDate = RAT.AllowedDate
This suppose to select a row by IdRoom, AccessToken, RefId, KeyCode and be above a certain date and not Expired.
Then get all chainded rows by ChainId and calculate time different between UTC_TIMESTAMP() and row ExpirationDate. (I guess it works till this point)
With that difference in minute, I want to calculate new datetimes but it doesn't work.
I've attached a screenshot with results
As you can see, it looks like NewAllowedDate and NewExpiringDate get calculated only for the first row.. But NewID gets calculated for all of them..
I can't understand where my query is wrong.
Any ideas?
Here the table and some data insert example
CREATE TABLE `ROOMS_ACCESS_TOKENS` ( `Id` int(11) NOT NULL, `IdRoom` int(11) DEFAULT NULL, `AccessToken` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL, `RefId` varchar(35) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL, `KeyCode` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL, `Allowed` int(1) NOT NULL, `Used` int(1) NOT NULL, `Expired` int(1) NOT NULL DEFAULT '0', `ChainId` int(11) NOT NULL DEFAULT '1', `StartDate` datetime DEFAULT NULL, `AllowedDate` datetime NOT NULL, `ExpirationDate` datetime DEFAULT NULL, `ResidenceTime` int(5) NOT NULL, `Url` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `ROOMS_ACCESS_TOKENS` (`Id`, `IdRoom`, `AccessToken`, `RefId`, `KeyCode`, `Allowed`, `Used`, `Expired`, `ChainId`, `StartDate`, `AllowedDate`, `ExpirationDate`, `ResidenceTime`, `Url`) VALUES (1, 1, '5092FFA0B3721AE5EC757649DE983F8020220104215810', 'B49F723F2FE0FD38063DA26F3EAEDBED', '2773217uysagkasd8wqejhjkasdsm', 1, 1, 0, 1, '2022-01-04 22:30:46', '2022-01-04 22:30:46', '2022-01-04 23:30:46', 60, '/it'), (2, 1, '9CAB5CABB559260D41366E1041B46D0320220104233046', 'B086A4B3F4996C69B4DBB206C2E0D799', '2773217uysagkasd8wqejhjkasdsm', 0, 1, 0, 1, '2022-01-04 22:30:46', '2022-01-04 23:31:46', '2022-01-05 00:31:46', 60, '/it'), (3, 1, '3A2AE9CAC195B9938D25AE654790FAE020220104233046', 'E756AF83048259114F7E0BA04FFA799E', '2773217uysagkasd8wqejhjkasdsm', 0, 1, 0, 1, '2022-01-04 22:30:46', '2022-01-05 00:32:46', '2022-01-05 01:32:46', 60, '/it'), (4, 1, '17267FAC80875252F5F1E2585D078B1D20220104233046', '62143086CF25D276FD4C6D4465BDD41E', '2773217uysagkasd8wqejhjkasdsm', 0, 1, 0, 1, '2022-01-04 22:30:46', '2022-01-05 01:33:46', '2022-01-05 02:33:46', 60, '/it'), (5, 1, '39F47036B85694A231668BB8DFBF3CD120220104233046', 'C2C8F545B9FF7C59D50958C14EE61BF7', '2773217uysagkasd8wqejhjkasdsm', 0, 1, 0, 1, '2022-01-04 22:30:46', '2022-01-05 02:34:46', '2022-01-05 03:34:46', 60, '/it'), (6, 1, '61919A70C0D7AD4FB642BE1A3D6FE10420220104233046', 'EB1F2AF642C779B29F89023260A3AC94', '2773217uysagkasd8wqejhjkasdsm', 0, 1, 0, 1, '2022-01-04 22:30:46', '2022-01-05 03:35:46', '2022-01-05 04:35:46', 60, '/it'), (7, 1, '62BCFCCAC7F2EAFD5B1E06B32A810C1420220104233046', 'DDFB911F2197B50861AABD77005D9946', '2773217uysagkasd8wqejhjkasdsm', 0, 1, 0, 1, '2022-01-04 22:30:46', '2022-01-05 04:36:46', '2022-01-05 05:36:46', 60, '/it'), (8, 1, '6F72CCB6F3DF8F90C1A2911A7F1E097720220104233046', 'CCD116F4D4EFA9AE7D9B8DA780C5A73E', '2773217uysagkasd8wqejhjkasdsm', 0, 1, 0, 1, '2022-01-04 22:30:46', '2022-01-05 05:37:46', '2022-01-05 06:37:46', 60, '/it'), (9, 1, 'E8610F5C7551DA2695360B77431BBB1020220104233046', '49ABCCBDF6FC02FF290357B6A29DB2FC', '2773217uysagkasd8wqejhjkasdsm', 0, 1, 0, 1, '2022-01-04 22:30:46', '2022-01-05 06:38:46', '2022-01-05 07:38:46', 60, '/it'), (10, 1, '78BFEAF3EC52298B9EB5CDA8920168FE20220104233046', '755E481F29003C1EDC3A682D5037EF9F', '2773217uysagkasd8wqejhjkasdsm', 0, 0, 0, 1, '2022-01-04 22:30:46', '2022-01-05 07:39:46', '2022-01-05 08:39:46', 60, '/it');
ALTER TABLE `ROOMS_ACCESS_TOKENS` ADD PRIMARY KEY (`Id`), ADD KEY `IX_ACCESS_TOKEN` (`AccessToken`), ADD KEY `KeyCode` (`KeyCode`), ADD KEY `RefId` (`RefId`), ADD KEY `IdRoom` (`IdRoom`), ADD KEY `Allowed` (`Allowed`), ADD KEY `Used` (`Used`), ADD KEY `Expired` (`Expired`);
ALTER TABLE `ROOMS_ACCESS_TOKENS` MODIFY `Id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=11;
These sound redundant. The former one is dynamic; the latter one is potentially not-yet-set.
AND RAT.ExpirationDate > UTC_TIMESTAMP()
AND RAT.Expired = 0
I do not understand your date arithmetic, especially since the columns, being DATETIME, have 'second' resolution, yet the arithmetic involved 'MINUTEs'.
What is the meaning of "ABS" in ABS(TIMESTAMPDIFF(MINUTE,T2.ExpirationDate, UTC_TIMESTAMP())) AS DifferenceTime
Please do not use the same alias twice; it confuses me, and maybe the parser. (I am thinking of RAT, at least.)
I have a sql statment I get the data from it to the datatable but what I need is filtering the data by dataview and create group y with sum the quantity for each group:
SELECT
FatoraReso as xAcc,
AccName AS xAccName,
FatoraDate AS xDate,
FatoraProduct as xProdID,
ProductName AS xProdName,
FatoraPurPrice as xPrice,
sum(FatoraQuan) as xQuan,
COUNT(FatoraID) AS xCarCount
from tblfatora
INNER JOIN tblaccounts ON tblaccounts.AccID = tblfatora.FatoraReso
INNER JOIN tblproducts ON tblproducts.ProductID = tblfatora.FatoraProduct
GROUP BY xAcc,xDate,xProdID,xPrice
UNION ALL
SELECT
FatoraCustomer as xAcc,
AccName AS xAccName,
FatoraDate AS xDate,
FatoraProduct as xProdID,
ProductName AS xProdName,
FatoraSalePrice as xPrice,
sum(FatoraQuan) as xQuan,
COUNT(FatoraID) AS xCarCount
from tblfatora
INNER JOIN tblaccounts ON tblaccounts.AccID = tblfatora.FatoraCustomer
INNER JOIN tblproducts ON tblproducts.ProductID = tblfatora.FatoraProduct
GROUP BY xAcc,xDate,xProdID,xPrice
ORDER BY xProdID,xPrice
I made a filter by the xAcc and xDate and then I didn't know how to apply group by with get the sum for each group.
I saw some solutions about using The LINQ but really I don't anything about LINQ.
so can help me please.
the final table that I want is:
+---------+--------+-------+-----------+
| xProdID | xPrice | xQuan | xCarCount |
+---------+--------+-------+-----------+
| 1 | 55 | 10 | 2 |
+---------+--------+-------+-----------+
| 1 | 60 | 15 | 1 |
+---------+--------+-------+-----------+
| 2 | 150 | 12 | 3 |
+---------+--------+-------+-----------+
| 2 | 155 | 11 | 2 |
+---------+--------+-------+-----------+
tblfatora:
CREATE TABLE `tblfatora` (
`FatoraID` bigint(20) NOT NULL,
`FatoraRef` bigint(20) NOT NULL,
`FatoraCode` varchar(50) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`FatoraDate` date NOT NULL,
`FatoraProduct` int(11) NOT NULL,
`FatoraQuan` double NOT NULL DEFAULT '0',
`FatoraReso` int(11) NOT NULL,
`FatoraPurPrice` double NOT NULL DEFAULT '0',
`FatoraPurTotal` double NOT NULL DEFAULT '0',
`FatoraCustomer` int(11) NOT NULL,
`FatoraSalePrice` double NOT NULL DEFAULT '0',
`FatoraDis` double NOT NULL DEFAULT '0',
`FatoraPlus` double NOT NULL DEFAULT '0',
`FatoraSaleTotal` double NOT NULL DEFAULT '0',
`FatoraDriverPayStatus` varchar(10) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`FatoraDriver` int(11) NOT NULL,
`FatoraCarNo` varchar(50) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`FatoraDriverCost` double NOT NULL DEFAULT '0',
`FatoraDriverCostTotal1` double NOT NULL DEFAULT '0',
`FatoraDriverCostTotal2` double NOT NULL DEFAULT '0',
`FatoraDriverPrice` double NOT NULL DEFAULT '0',
`FatoraDetails1` varchar(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
`FatoraDetails2` varchar(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
`FatoraDetails3` varchar(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
`FatoraPalletQuan` double NOT NULL DEFAULT '0',
`FatoraPalletPrice` double NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ROW_FORMAT=COMPACT;
tblFatoraData:
INSERT INTO `tblfatora` (`FatoraID`, `FatoraRef`, `FatoraCode`, `FatoraDate`,
`FatoraProduct`, `FatoraQuan`, `FatoraReso`, `FatoraPurPrice`,
`FatoraPurTotal`, `FatoraCustomer`, `FatoraSalePrice`, `FatoraDis`,
`FatoraPlus`, `FatoraSaleTotal`, `FatoraDriverPayStatus`, `FatoraDriver`,
`FatoraCarNo`, `FatoraDriverCost`, `FatoraDriverCostTotal1`,
`FatoraDriverCostTotal2`, `Fato`raDriverPrice`, `FatoraDetails1`,
`FatoraDetails2`, `FatoraDetails3`, `FatoraPalletQuan`, `FatoraPalletPrice`)
VALUES
(1, 202010180304112, '', '2020-10-01', 41, 31, 112, 71, 2201, 93, 71, 0, 0,
2201, 'Paid', 128, '135538', 0, 0, 0, 1, '', '', '', 0, 0),
(2, 202010180720343, '', '2020-10-01', 43, 40, 112, 61, 2440, 83, 68000, 0,
0, 2720000, 'Paid', 129, '', 0, 0, 0, 1, '', '', '', 0, 0),
(3, 202010180807273, '', '2020-10-01', 43, 34, 112, 61, 2074, 341, 72000, 0,
0, 2448000, 'Paid', 130, '8142', 0, 0, 0, 1, '', '', '', 0, 0),
(4, 202010180819273, '', '2020-10-01', 50, 40, 114, 70, 2800, 76, 70, 0, 0,
2800, 'Paid', 131, '', 0, 0, 0, 1250, '', '', '', 0, 0),
(5, 202010180821373, '', '2020-10-01', 55, 39.22, 114, 66, 2588.52, 55, 66,
0, 0, 2588.52, 'Paid', 132, '', 0, 0, 0, 1250, '', '', '', 0, 0),
(6, 202010180823183, '', '2020-10-01', 50, 38, 114, 70, 2660, 360, 70, 0, 0,
2660, 'Paid', 133, '', 0, 0, 0, 1, '', '', '', 0, 0),
(7, 202010180825173, '', '2020-10-01', 50, 39, 114, 70, 2730, 67, 64, 0, 14,
2510, 'Paid', 134, '', 0, 0, 0, 1250, '', '', '', 0, 0),
(8, 202010180832333, '', '2020-10-02', 48, 31, 113, 63, 1953, 64, 56, 0, 0,
1736, 'Paid', 135, '', 0, 0, 0, 1240, '', '', '', 0, 0),
(9, 202010180833593, '', '2020-10-01', 48, 35, 113, 63, 2205, 82, 63, 0, 0,
2205,
'Paid', 136, '', 0, 0, 0, 1, '', '1', '', 0, 0),
(10, 202010180838003, '', '2020-10-01', 35, 35.46, 115, 57, 2021.22, 53, 56,
0, 0, 1985.76, 'Paid', 137, '135280', 0, 0, 0, 1240, '', '', '', 0, 0),
(11, 202010180841213, '', '2020-10-02', 43, 33, 112, 61, 2013, 79, 58, 0, 0,
1914, 'Paid', 138, '', 0, 0, 0, 1240, '', '', '', 0, 0),
(12, 202010180843373, '', '2020-10-02', 43, 39, 112, 61, 2379, 84, 70000, 0,
0, 2730000, 'Paid', 139, '10978', 0, 0, 0, 1, '', '', '', 0, 0),
(13, 202010180846253, '', '2020-10-02', 43, 39, 112, 61, 2379, 225, 56, 0, 0,
2184, 'Paid', 140, '9705', 0, 0, 0, 1240, '', '', '', 0, 0),
(14, 202010180847593, '', '2020-10-02', 43, 40, 112, 61, 2440, 344, 61, 0, 0,
2440, 'Paid', 141, '97464', 0, 0, 0, 1, '', '', '', 0, 0),
(15, 202010180849563, '', '2020-10-02', 43, 36, 112, 61, 2196, 73, 57, 0, 0,
2052, 'Paid', 142, '', 0, 0, 0, 1240, '', '', '', 0, 0),
(16, 202010180852113, '', '2020-10-02', 43, 39, 112, 61, 2379, 357, 61, 0, 0,
2379, 'Paid', 143, '151957', 0, 0, 0, 1, '', '', '', 0, 0)
You can use every Query as basis (subquery) too do more calculations
SELECT
xProdID,
xPrice,
xProdName,
SUM(xQuan) AS xQuan,
SUM(xCarCount) AS xCarCount
FROM (SELECT
FatoraReso AS xAcc,
AccName AS xAccName,
FatoraDate AS xDate,
FatoraProduct AS xProdID,
ProductName AS xProdName,
FatoraPurPrice AS xPrice,
SUM(FatoraQuan) AS xQuan,
COUNT(FatoraID) AS xCarCount
FROM
tblfatora
INNER JOIN
tblaccounts ON tblaccounts.AccID = tblfatora.FatoraReso
INNER JOIN
tblproducts ON tblproducts.ProductID = tblfatora.FatoraProduct
GROUP BY xAcc , xDate , xProdID , xPrice
UNION ALL SELECT
FatoraCustomer AS xAcc,
AccName AS xAccName,
FatoraDate AS xDate,
FatoraProduct AS xProdID,
ProductName AS xProdName,
FatoraSalePrice AS xPrice,
SUM(FatoraQuan) AS xQuan,
COUNT(FatoraID) AS xCarCount
FROM
tblfatora
INNER JOIN
tblaccounts ON tblaccounts.AccID = tblfatora.FatoraCustomer
INNER JOIN
tblproducts ON tblproducts.ProductID = tblfatora.FatoraProduct
GROUP BY xAcc , xDate , xProdID , xPrice) t1
GROUP BY
xProdID , xPrice
ORDER BY xProdID , xPrice
As i wrote in my comment.
''Isee two possibility to get only the dates for one or more date.
The selection depends whetger you have a fast connection and a fast server.
Solution 1;:
You get all data into a table and have a dataview t filter your wanted dates.
then have a Sub which sums up the data according to xProdID , xPrice.
That is suitable if you a slow connection or a slow database server, so that the computer does it all on client side.
Solution 2
You send to the server a new SELECT as posted abouve only you add a where clause which only selects the appropriate dates before the final GROUP BY and SUM.
this works just fine, when all is quite fast.
The aggregation functions lose data, because it has only the max date, this may be enugh, but i don#t believe that it will suffice
I messed up big time: I added about 2000 images to Phoca Gallery and instead of leaving the "title" field empty, so it would use the filenames as the title, I wrote the name of the category... Renaming each one manually would be a PITA, but I'm sure it can be done in SQL, except I don't really know how.
What I have now is:
(15574, 1379, 0, 'Thursday, 25.6.', 'thursday-25-6', 'competitions/hrprvprj/25.6/120/vucemilo_filip/_MG_5545.jpg', 1, NULL, '2015-07-01 16:55:11', 0, '', '', 0, '', 0, NULL, 0, 212111, 1, 1, 0, '0000-00-00 00:00:00', 13, NULL, NULL, NULL, NULL, NULL, NULL, '', 0, '', '', '', '', '', '', '*'),
and what I'd need is:
(15574, 1379, 0, '_MG_5545.jpg', '_mg_5545.jpg', 'competitions/hrprvprj/25.6/120/vucemilo_filip/_MG_5545.jpg', 1, NULL, '2015-07-01 16:55:11', 0, '', '', 0, '', 0, NULL, 0, 212111, 1, 1, 0, '0000-00-00 00:00:00', 13, NULL, NULL, NULL, NULL, NULL, NULL, '', 0, '', '', '', '', '', '', '*'),
so I'd replace "Thursday, 25.6" and it's alias "thursday-25-6" with the filename of each image, which is the last 12 digits of the column with the image location.
Is there any string I could enter in phpMyAdmin that would do the trick?
Thank you so much!
If the pattern is same i.e.
competitions/hrprvprj/25.6/120/vucemilo_filip/_MG_5545.jpg'
Then you may use the substring_index function and using this you do not have to rely on 12 characters rather you can extract the last part of string after the last / something as
mysql> select substring_index('competitions/hrprvprj/25.6/120/vucemilo_filip/_MG_5545.jpg','/',-1) as i ;
+--------------+
| i |
+--------------+
| _MG_5545.jpg |
+--------------+
1 row in set (0.00 sec)
mysql> select lower(substring_index('competitions/hrprvprj/25.6/120/vucemilo_filip/_MG_5545.jpg','/',-1)) as i ;
+--------------+
| i |
+--------------+
| _mg_5545.jpg |
+--------------+
1 row in set (0.00 sec)
So you may have the update command as
update tablename
set
col1 = substring_index(col_file_path,'/',-1),
col2 = lower(substring_index(col_file_path,'/',-1));
You can use the substring function to extract the last 12 characters of the string, and from there on it's just a plain old update:
UPDATE mytable
SET title = SUBSTRING(filename, -12),
alias = LOWER(SUBSTRING(filename, -12))
WHERE <some condition>
{
“id”: 0,
“isfname”: null,
“islname”: null,
“csfname”: null,
“cslname”: null,
“snf”: null,
“dateadmit”: null,
“csaddress”: null,
“cscsz”: null,
“cstelephone”: null,
“csemail”: null,
“datemaneed”: null,
“csownrent”: null,
“csage”: 0,
“isage”: 0,
“isincome”: [
{
“type”: null,
“gross”: 0,
“deducts”: 0,
“net”: 0
}
],
“csincome”: [
{
“type”: null,
“gross”: 0,
“deducts”: 0,
“net”: 0
}
],
“banks”: [
{
“type”: null,
“desc”: null,
“owner”: null,
“amt”: 0
}
],
“inv”: [
{
“type”: null,
“desc”: null,
“owner”: null,
“amt”: 0
}
],
“csira”: 0,
“isira”: 0,
“csli”: [
{
“type”: null,
“desc”: null,
“cv”: 0,
“fv”: 0
}
],
“isli”: [
{
“type”: null,
“desc”: null,
“cv”: 0,
“fv”: 0
}
],
“veh”: [
{
“type”: null,
“makemodel”: null,
“amt”: 0
}
],
“realty”: [
{
“desc”: null,
“home”: true,
“amt”: 0
}
],
“totals”: [
{
“banks”: 0,
“inv”: 0,
“csli”: 0,
“isli”: 0,
“veh”: 0,
“realty”: 0,
“exemptli”: 0,
“exemptira”: 0,
“exempthome”: 0,
“totalgross”: 0,
“totalexempt”: 0,
“totalcountable”: 0,
“csra”: 0,
“isretained”: 0,
“totalretained”: 0,
“totalspenddown”: 0
}
],
“shelters”: {
“rent”: 0,
“mortgage”: 0,
“retaxes”: 0,
“hoins”: 0,
“sua”: 0,
“shelterstd”: 0
},
“ismedins”: 0,
“isincavail”: 0,
“isinctosnf”: 0,
“isinctocs”: 0,
“spenddown”: {
“snfbill”: 0,
“legal”: 0,
“mortgage”: 0,
“ppf”: 0,
“newvehicle”: 0,
“repairs”: 0
},
“otherspenddown”: [
{
“annuity”: 0,
“other”: null
}
],
“manos”: {
“minmmna”: 0,
“maxmmna”: 0,
“mincsra”: 0,
“maxcsra”: 0,
“pna”: 0,
“incomelimit”: 0
}
}
The quotes are all wrong:
“islname”
Should be
"islname"
Did you copy it from MS Word document or something? Don't do that.
according to JSON parser: Unrecognized token '?'
http://json.parser.online.fr
Bad quotes.