1
I have two tables: company and walking
In the table walk, I have the input date, date, value I would like to select all companies, and list it and the total amount the company spent on daily rates, only if the date is different from '0000-00-00 00:00:00' in the case. If you haven’t completed the daily rate, like 1 day and 20 hours for example, the daily rate is counted equal. Always rounded up.
I am using PHP. I’m stuck in this SQL that needs to first find out how many daily gave in total for each truck, then make times the daily value, and then add up all the totals and generate the total that was spent by each company.
$sql = mysql_query("SELECT * FROM empresa WHERE emp_ativo = '1' ORDER BY emp_nome ASC");
while ($linha = mysql_fetch_assoc($sql)){
$sql_valor = mysql_query("SELECT * FROM caminhao WHERE cam_ativo = '1' AND emp_id = '".$linha['emp_id']."' AND cam_datasaida != '0000-00-00 00:00:00'");
while ($val = mysql_fetch_array($sql_valor)){
...
}
$lista[] = $linha;
}
post tables templates, preferably on Sqlfiddle to build the query
– Caputo
I would make a Function , which would receive input date and date output and calculate the number of days (rounding) select would be simpler select id,sum(qtd_days(input date, date output)) qtdduas from caminhao group by id , not publish as solution as Function itself is missing.
– Motta
You’d better pass the templates like Caputo said, but maybe what you want is something like: SELECT e.emp_id, e.emp_name, SUM(c.cam_valordiaria) AS total_diarias FROM company e JOIN caminhao c ON c.cam_active = '1' AND c.emp_id = e.emp_id AND e.cam_dataoutput != '0000-00-00 00:00:00' WHERE e.emp_active = '1' GROUP BY (e.emp_id) ORDER BY e.emp_asc name
– vmartins