如何通过加入以下查询来进行单个查询:
查询1:
$sql = "SELECT total_ce FROM mfb_agent_status_summary AS agent LEFT JOIN mfb_servicelog AS service ON agent.sl_id = service.sl_id WHERE service.h_id LIKE ('".$value[$i]."') AND service_add_date >= DATE_FORMAT('" . $from . "', '%Y-%m-%d') AND service_add_date <= DATE_FORMAT('" . $to . "', '%Y-%m-%d')";
查询2:
$query="SELECT DATE_FORMAT(service_add_date, '%d/%b/%y'), DATE_FORMAT(scan_date, '%d/%b/%y'), DATE_FORMAT(ftp_date, '%d/%b/%y'),no_of_cases_reported,no_of_cases_received,case_count_diff, DATEDIFF(ftp_date, service_add_date) FROM mfb_servicelog WHERE h_id LIKE ('".$value[$i]."') AND service_add_date >= DATE_FORMAT('" . $from . "', '%Y-%m-%d') AND service_add_date <= DATE_FORMAT('" . $to . "', '%Y-%m-%d')";
谢谢!
答案 0 :(得分:1)
我在这里对你的架构做了一些假设,但尝试这样的事情:
SELECT agent.total_ce,
DATE_FORMAT(service.service_add_date, '%d/%b/%y'),
DATE_FORMAT(service.scan_date, '%d/%b/%y'),
DATE_FORMAT(service.ftp_date, '%d/%b/%y'),
service.no_of_cases_reported,
service.no_of_cases_received,
service.case_count_diff,
DATEDIFF(service.ftp_date, service.service_add_date)
FROM mfb_agent_status_summary AS agent
LEFT JOIN mfb_servicelog AS service ON agent.sl_id = service.sl_id
WHERE service.h_id LIKE ('".$value[$i]."') AND
agent.service_add_date >= DATE_FORMAT('" . $from . "', '%Y-%m-%d') AND
agent.service_add_date <= DATE_FORMAT('" . $to . "', '%Y-%m-%d')
我还想提一下,您应该真正关注prepared statements,而不是直接将$from
和$to
注入您的查询。你正在打开潜在的SQL injection攻击。
我强烈建议您使用PHP PDO library而非mysqli_*
函数。
答案 1 :(得分:0)
所以我的猜测是:
$sql = "SELECT
agent.total_ce,
service.*
FROM mfb_agent_status_summary AS agent
LEFT JOIN (
SELECT
DATE_FORMAT(service_add_date, '%d/%b/%y'),
DATE_FORMAT(scan_date, '%d/%b/%y'),
DATE_FORMAT(ftp_date, '%d/%b/%y'),
no_of_cases_reported,
no_of_cases_received,
case_count_diff,
DATEDIFF(ftp_date, service_add_date)
FROM mfb_servicelog
WHERE h_id LIKE ('".$value[$i]."')
AND service_add_date >= DATE_FORMAT('" . $from . "', '%Y-%m-%d')
AND service_add_date <= DATE_FORMAT('" . $to . "', '%Y-%m-%d')
) AS service
ON agent.sl_id = service.sl_id";
我认为你可以用&#39; INNER JOIN&#39;;
替换LEFT JOIN