所以我曾經有10個查詢,相同的SELECT語句,不同的WHERE。一個查詢是這樣的:MYSQL查詢,其中SELECT語句(要多行/ where語句)
SELECT
workdate AS `Date`,
DAYNAME(workdate) AS `Day`,
COALESCE(TRUNCATE(SUM((CASE
WHEN
paycode = '03 Overtime 2.0'
then
(IF(historyemployeepay.`Other Rate` = 0,
(employeehours * (historyemployeepay.`Base Rate` * 2.0)),
(employeehours * (historyemployeepay.`Other Rate` * 2.0))))
WHEN paycode = '01 Ordinary' then (employeehours * historyemployeepay.`Base Rate`)
WHEN
paycode = '02 Overtime 1.5'
OR paycode = '03 Overtime 2.0'
then
(IF(historyemployeepay.`Other Rate` = 0,
(employeehours * (historyemployeepay.`Base Rate` * 1.5)),
(employeehours * (historyemployeepay.`Other Rate` * 1.5))))
end)),
2),
0) AS `Amount`
FROM
otherrates,
employeedata
INNER JOIN
employeehours ON employeedata.`ID Number` = employeehours.employeeid
JOIN
historyemployeepay ON employeehours.employeeid = historyemployeepay.EmployeeID
WHERE
shift = 'PM' AND DriverDock = 'Driv'
AND `Salary Code Description` LIKE '%Perm%'
AND DAYNAME(workdate) <> 'Saturday'
AND historyemployeepay.FromDate <= employeehours.workdate
AND (historyemployeepay.ToDate IS NULL
OR employeehours.workdate <= historyemployeepay.ToDate)
GROUP BY workdate
我想有他們所有網站頁面上,如果我這樣做單獨的網站頁面需要年齡加載...所以我想到了一個好主意的嘗試將它們鏈接在一起,如下所示。請注意我在SELECT語句中取出了八行,僅留下了兩個例子。
我該如何得到這個工作?它不顯示正確的值(空),我做錯了什麼?
SELECT
workdate AS `Date`,
DAYNAME(workdate) AS `Day`,
(CASE
WHEN
shift = 'AM' AND DriverDock = 'Driv'
AND `Salary Code Description` LIKE '%Perm%'
then
COALESCE(TRUNCATE(SUM((CASE
WHEN
paycode = '03 Overtime 2.0'
then
(IF(historyemployeepay.`Other Rate` = 0,
(employeehours * (historyemployeepay.`Base Rate` * 2.0)),
(employeehours * (historyemployeepay.`Other Rate` * 2.0))))
WHEN paycode = '01 Ordinary' then (employeehours * historyemployeepay.`Base Rate`)
WHEN
paycode = '02 Overtime 1.5'
OR paycode = '03 Overtime 2.0'
then
(IF(historyemployeepay.`Other Rate` = 0,
(employeehours * (historyemployeepay.`Base Rate` * 1.5)),
(employeehours * (historyemployeepay.`Other Rate` * 1.5))))
end)),
2),
0)
end) AS `AMPERMDRIV`,
(CASE
WHEN
shift = 'AM' AND DriverDock = 'Dock'
AND `Salary Code Description` LIKE '%Perm%'
then
COALESCE(TRUNCATE(SUM((CASE
WHEN
paycode = '03 Overtime 2.0'
then
(IF(historyemployeepay.`Other Rate` = 0,
(employeehours * (historyemployeepay.`Base Rate` * 2.0)),
(employeehours * (historyemployeepay.`Other Rate` * 2.0))))
WHEN paycode = '01 Ordinary' then (employeehours * historyemployeepay.`Base Rate`)
WHEN
paycode = '02 Overtime 1.5'
OR paycode = '03 Overtime 2.0'
then
(IF(historyemployeepay.`Other Rate` = 0,
(employeehours * (historyemployeepay.`Base Rate` * 1.5)),
(employeehours * (historyemployeepay.`Other Rate` * 1.5))))
end)),
2),
0)
end) AS `AMPERMDOCK`
FROM
otherrates,
employeedata
INNER JOIN
employeehours ON employeedata.`ID Number` = employeehours.employeeid
JOIN
historyemployeepay ON employeehours.employeeid = historyemployeepay.EmployeeID
WHERE
DAYNAME(workdate) <> 'Saturday'
AND historyemployeepay.FromDate <= employeehours.workdate
AND (historyemployeepay.ToDate IS NULL
OR employeehours.workdate <= historyemployeepay.ToDate)
GROUP BY workdate
看一看結果:
爲什麼對SUM集合外部的'shift','DriverDock'進行條件測試? ......是否真的想要評估那些聚合後的人? (對我來說不正確)。在評估可能返回NULL的CASE後,爲什麼沒有執行COALESCE和TRUNCATE?爲什麼對'otherrates'表的CROSS JOIN操作,它是否只包含一行,以及爲什麼連接的逗號語法?給這些表分配簡短的別名可以使合格的列引用更短,並且許多列引用不合格,我們無法確定這些列來自哪個表; – spencer7593
爲什麼第二次測試'03加班2.0',這已經在早些時候被抓到了? – spencer7593