MYSQL SUM函数用和里面

时间:2016-06-17 17:20:45

标签: mysql laravel-5

我正在进行查询,以便在左侧导航时立即获得多个计数。我总结了匹配每个条件的行数并将该数字传递给左侧导航的DOM,但是其中两个SUMS比其他SUMS更复杂并且返回0,即使我知道有行中的行与表达式匹配的DB。

$processingcounts = $second->select(DB::raw(' 
        SUM(scheduled_in != "") as processingPreRepairs,
        SUM(in_arrival = 1) as processingNewArrivals, 
        SUM(delivery_complete = 1) as processingDelivered, 
        SUM(supplement = 1) as processingSupplement, 
        SUM(scheduled_in >= '.$start.' and scheduled_in <= '.$end.') as processingCurrentWeek, 
        SUM(scheduled_in >= '.$nextStart.' and scheduled_in <= '.$nextEnd.') as processingNextWeek, 
        SUM(file_location = "SCHEDULED") as processingScheduledList, 
        SUM(file_location = "IN-SHOP") as processingInShop, 
        SUM(file_location = "OFFICE") as processingOffice 
        '))->first();

所有其他SUMS都返回正确的值,但是我称之为的两个SUMS&#34; processingCurrentWeek&#34; && 34; processingNextWeek&#34;正在退回0,尽管他们应该分别返回2和3。我的问题是你可以像我一样使用SUM,基本上就是在它们里面。

编辑:

当我转储laravel查询时,这是带有日期的查询输出。

SUM(scheduled_in != "") as processingPreRepairs,
SUM(in_arrival = 1) as processingNewArrivals, 
SUM(delivery_complete = 1) as processingDelivered, 
SUM(supplement = 1) as processingSupplement, 
SUM(scheduled_in >= 2016-06-13 and scheduled_in <= 2016-06-17) as processingCurrentWeek, 
SUM(scheduled_in >= 2016-06-20 and scheduled_in <= 2016-06-24) as processingNextWeek, 
SUM(file_location = "SCHEDULED") as processingScheduledList, 
SUM(file_location = "IN-SHOP") as processingInShop, 
SUM(file_location = "OFFICE") as processingOffice 

1 个答案:

答案 0 :(得分:1)

日期必须包装为字符串。他们现在解释为表达式:2016-06-10 = 2000

您可以使用占位符:

$processingcounts = $second->select(' 
    SUM(scheduled_in != "") as processingPreRepairs,
    SUM(in_arrival = 1) as processingNewArrivals, 
    SUM(delivery_complete = 1) as processingDelivered, 
    SUM(supplement = 1) as processingSupplement, 
    SUM(scheduled_in >= ? and scheduled_in <= ?) as processingCurrentWeek, 
    SUM(scheduled_in >= ? and scheduled_in <= ?) as processingNextWeek, 
    SUM(file_location = "SCHEDULED") as processingScheduledList, 
    SUM(file_location = "IN-SHOP") as processingInShop, 
    SUM(file_location = "OFFICE") as processingOffice 
    ', [$start, $end, $nextStart, $nextEnd])->first();