使用 TIMEDIFF 和 ADDTIME 时,查询的特定部分出现问题:
我想得到这样的结果:
| waktuhilang |
|-------------|
| 04:00:00 |
| 03:00:00 |
SELECT
ADDTIME(
(
SELECT
SEC_TO_TIME(
SUM(
TIME_TO_SEC(
maketime(
durasi + 0,
substring_index(durasi, 'Jam ', - 1) + 0,
0
)
)
)
) waktudw
FROM
trans_lhpdtdw
),
(
SELECT
TIMEDIFF(JamMasuk, JamMulai) lamaistirahat
FROM
trans_lhphd
)
) waktuhilang
我看到有些人提到在子查询中使用IN代替=,但我似乎无法让它工作。有什么想法吗?感谢
我有两张桌子:
Table trans_lhpdtdw Table trans_lhphd
CREATE TABLE `trans_lhpdtdw` (
`IdBukti` int(11) NOT NULL,
`PartID` varchar(50) NOT NULL,
`TypeDowntime` varchar(50) NOT NULL,
`Durasi` varchar(50) NOT NULL,
`KeteranganDowntime` longtext NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- ----------------------------
-- Records of trans_lhpdtdw
-- ----------------------------
INSERT INTO `trans_lhpdtdw` VALUES ('2', 'BLAP-FG150-KT-KGX', 'SETTING MOLD', '1 Jam 0 Menit', 'ass');
INSERT INTO `trans_lhpdtdw` VALUES ('1', 'BLAP-FG152-PF-KGX', 'TIDAK ADA FASILITAS', '1 Jam 0 Menit', 'as');
INSERT INTO `trans_lhpdtdw` VALUES ('1', 'AWDX-FG002-HN-KGX', 'TIDAK ADA JOB', '2 Jam 0 Menit', 'sasa');
CREATE TABLE `trans_lhphd` (
`IdBukti` int(11) NOT NULL AUTO_INCREMENT,
`NoBukti` varchar(1000) NOT NULL,
`Periode` int(6) NOT NULL,
`Tanggal` date NOT NULL,
`Divisi` varchar(50) NOT NULL,
`JamKerja` varchar(10) NOT NULL,
`JamProduksi` time NOT NULL,
`JamSelesai` time NOT NULL,
`JamMulai` time NOT NULL,
`JamMasuk` time NOT NULL,
`IdMesin` varchar(50) NOT NULL,
`LineID` varchar(50) NOT NULL,
`Operator` int(5) NOT NULL,
`Planning` decimal(18,0) NOT NULL,
`Tambahan` varchar(50) DEFAULT NULL,
PRIMARY KEY (`IdBukti`),
KEY `IdBukti` (`IdBukti`),
KEY `IdBukti_2` (`IdBukti`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
-- ----------------------------
-- Records of trans_lhphd
-- ----------------------------
INSERT INTO `trans_lhphd` VALUES ('1', 'LHP/201708/00001', '201708', '2017-08-24', 'ALL DIVISI IMPORT (NON PPIC)', 'Shift I', '01:05:00', '09:05:00', '01:00:00', '02:00:00', 'BH 260K', 'L3002', '1', '213', '1 Jam 15 Menit');
INSERT INTO `trans_lhphd` VALUES ('2', 'LHP/201708/00002', '201708', '2017-08-17', 'INJECTION', 'Shift I', '07:00:00', '16:00:00', '11:25:00', '13:25:00', 'BH 260K', 'L2002', '100', '1000', '');
答案 0 :(得分:0)
你的问题是第二个子查询。我猜你想要:
SELECT ADDTIME( (SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(maketime(durasi + 0,
substring_index(durasi, 'Jam ', - 1) + 0, 0
)
)
)
)
FROM trans_lhpdtdw
),
TIMEDIFF(JamMasuk, JamMulai)
) as waktuhilang
FROM trans_lhphd;
答案 1 :(得分:0)
假设您只想查询返回的一个值,请调整第二个子查询以返回TIMEDIFF计算的SUM。
另请参阅此SQL Fiddle
查询1 :
SELECT
ADDTIME(
(
SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(maketime(durasi + 0, substring_index(durasi, 'Jam ', - 1) + 0, 0)))) waktudw
FROM trans_lhpdtdw
)
, (
SELECT SUM(TIMEDIFF(JamMasuk, JamMulai))
FROM trans_lhphd
)
) waktuhilang
<强> Results 强>:
| waktuhilang |
|-------------|
| 07:00:00 |
答案 2 :(得分:0)
为每个IdBukti
提供一行的方法<强>查询强>:
SELECT
d.IdBukti, ADDTIME(w.waktudw, TIMEDIFF(d.JamMasuk, d.JamMulai)) waktuhilang
FROM trans_lhphd as d
INNER JOIN (
SELECT IdBukti, SEC_TO_TIME(SUM(TIME_TO_SEC(maketime(durasi + 0, substring_index(durasi, 'Jam ', - 1) + 0, 0)))) waktudw
FROM trans_lhpdtdw
GROUP BY IdBukti
) as w ON d.IdBukti = w.IdBukti
<强> Results 强>:
| IdBukti | waktuhilang |
|---------|-------------|
| 1 | 04:00:00 |
| 2 | 03:00:00 |