当我按列对表格进行排序时,我发现我的数字排序错误,因为我的数字是字符串。我找到了一种 CAST(COALESCE(p.total,0)AS UNSIGNED)的解决方案,现在它以整数形式返回它们。我认为我需要对所有数字进行此操作非常不方便?有人可以解释一下为什么它会那样或我做错了吗?
$result = DB::select(DB::raw(
"SELECT i.date,
u.type,
CONCAT_WS(' ', u.first_name, u.last_name) AS consultant,
c.name AS customer,
CAST(COALESCE(e.pp,0) AS UNSIGNED) AS pp,
CAST(COALESCE(e.sp,0) AS UNSIGNED) AS sp,
CAST(COALESCE(e.sp,0) - COALESCE(e.pp,0) AS UNSIGNED) AS margin,
CAST(COALESCE(p.total,0) AS UNSIGNED) AS total_purchase,
CAST(COALESCE(i.total, 0) AS UNSIGNED) AS total_sales,
CAST(COALESCE(i.total,0) - COALESCE(p.total,0) AS UNSIGNED) AS gross_margin
FROM clockwork.invoices AS i
INNER JOIN clockwork.timesheets AS t ON i.timesheet_id = t.id
LEFT OUTER JOIN clockwork.purchases AS p ON t.id = p.timesheet_id
INNER JOIN clockwork.users AS u ON i.user_id = u.id
INNER JOIN clockwork.customers AS c ON i.customer_id = c.id
LEFT OUTER JOIN clockwork.contract_extensions AS e ON i.extension_id = e.id
WHERE i.date between '$request->start' and '$request->end'
GROUP BY i.date, u.type, u.first_name, u.last_name, c.name, e.pp, e.sp, p.total, i.total
ORDER BY u.first_name
"));