I have a datatable being populated bt one table, but also use a 2nd table to run a conditional Sum(case when...)
The initial query which works fine is:
$sql = "SELECT `add_job`.*,
SUM(addincome) AS income,
SUM(addpayment) AS cost,
SUM(`add_job`.`addincome` - `add_job`.`addpayment`) / SUM(`add_job`.`addpayment`)*100 AS markup,
SUM((add_job.addincome * 1.2) - add_job.addincome) AS vat_in
FROM`add_job`";
$query = mysqli_query($conn, $sql);
I have another query which works fine on its own but when I try to join it into the above, it breaks the table and shows no data
"SELECT `driver`.*,
SUM(CASE WHEN `driver`.vatregistered= 'Y' then `add_job`.addpayment Else 0 end) * 0.2 as vat_pay
FROM `driver`
LEFT JOIN `add_job` ON `add_job`.adddriver = `driver`.drivercompany";
I run these queries through PHP and populate the datatables with the outcomes.
I would very much appreciate of someone could help me join the two queries
Incase anyone comes up against this sort of thing going forward... I grouped together as so:
$sql = "SELECT *, FROM (SELECT `add_job`.*,
SUM(addincome) AS income,
SUM(addpayment) AS cost,
SUM(`add_job`.`addincome` - `add_job`.`addpayment`) / SUM(`add_job`.`addpayment`)*100 AS markup,
SUM((add_job.addincome * 1.2) - add_job.addincome) AS vat_in
FROM `add_job`) q1
join
(SELECT `driver`.*,
SUM(CASE WHEN `driver`.vatregistered= 'Y' then `add_job`.addpayment Else 0 end) * 0.2 as vat_pay
FROM `driver`
LEFT JOIN `add_job` ON `add_job`.adddriver = `driver`.drivercompany) q2 on 0=0";
$query = mysqli_query($conn, $sql);
Where q1 in the answer refers to Query 1 in question and q2 to Query 2.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.