SELECT total_power_requirement, number_of_slots, location,
elevation, title, circuit_breaker, power_panel FROM racks
LEFT JOIN (
SELECT rack_id,
SUM(power_requirement) AS total_power_requirement
FROM devices
GROUP BY rack_id) AS tpr ON tpr.rack_id = racks.rack_id
WHERE racks.rack_id = 2
SELECT tpr.total_power_requirement
, x.records
, racks.number_of_slots
, racks.location
, racks.elevation
, racks.title
, racks.circuit_breaker
, racks.power_panel
FROM racks
CROSS
JOIN ( SELECT COUNT(*) AS records
FROM devices ) AS x
LEFT
JOIN ( SELECT rack_id
, SUM(power_requirement) AS total_power_requirement
FROM devices
GROUP
BY rack_id ) AS tpr
ON tpr.rack_id = racks.rack_id
WHERE racks.rack_id = 2
…
In MySQL, JOIN , CROSS JOIN , and INNER JOIN are syntactic equivalents (they can replace each other). In standard SQL, they are not equivalent. INNER JOIN is used with an ON clause, CROSS JOIN is used otherwise
…
your WHERE condition on the racks table , and the joined tps.rack_id = racks.rack_id data in the tpr subquery, both of these will return results for only one rack
I didnt realize you can put a where clause in the cross join,
$sql = "SELECT tpr.total_power_requirement
, x.records
, racks.number_of_slots
, racks.location
, racks.elevation
, racks.title
, racks.circuit_breaker
, racks.power_panel
FROM racks
CROSS
JOIN ( SELECT COUNT(*) AS records
FROM devices WHERE rack_id = ".$rack_id.") AS x
LEFT
JOIN ( SELECT rack_id
, SUM(power_requirement) AS total_power_requirement
FROM devices
GROUP
BY rack_id ) AS tpr
ON tpr.rack_id = racks.rack_id
WHERE racks.rack_id = ".$rack_id;