是否可以从MySQL中的多个NAME TREATMENT DATE week0 week1 week2 week3 week4 week5 week6 week7
CCG1 Treatment1 APR16 1 1 3 2 4 0 0 0
CCG1 Treatment2 APR16 0 0 2 12 0 3 5 0
查询中排序结果?
SELECT
计算每周付费的司机。我有14个驱动程序的列表,我想在一个查询中显示所有14个驱动程序的结果。
结果我得到:
SELECT (
SELECT COUNT(ID) FROM `Bookings` WHERE
PickUpDate between Date1 And Date2 And `Driver`= Drivers) AS Total_Jobs,
(
SELECT Sum(`Value`) FROM `Bookings` WHERE
PickUpDate between Date1 And Date2 And `Driver`= Drivers) AS Total_Value,
(
SELECT Total_Jobs * 5 ) AS Fiver_Value,
(
SELECT Total_Value - Fiver_Value ) AS Payment_Due
答案 0 :(得分:0)
All those selects are just plain wrong - you need to read up on sql aggregation functions. The query should look more like this
select driverid, count(*) as total_jobs, sum(value),
count(*) * 5 as fiver_value, sum(value) - (count(*) * 5) as paymen_due
from bookings
where PickUpDate between Date1 And Date2
group by driverid
You could help yourself by adding sample data as text to the question or to sqlfiddle.
答案 1 :(得分:0)
如果多个选择具有相同的字段并稍后设置顺序,则可以使用UNION。 SELECT * FROM (选择一个FROM t1 WHERE a = 10 AND B = 1 ORDER by LIMIT 10) 联盟 (选择一个FROM t2 WHERE a = 11 AND B = 2 ORDER by LIMIT 10);
订购