Hello all,
A friend made this query for me in order to get a part of my page working
$queryCategory = "SELECT *,(SELECT dc.id FROM drive_cargotype dc, drive_routes dr WHERE dc.id = dr.cargotype AND dr.id=? ORDER BY dr.id DESC LIMIT 1) as cargo FROM drive_cargotype";
$stmt = $db->prepare($queryCategory);
$stmt->execute(array($_GET['id']));
$categories = $stmt->fetchAll();
However, he didn't give me an explanation on how this query actually works. With my knowledge I understand some parts of it, but I don't know where to adapt the query so that it's also applicable for other sections of the page.
Is there anyone who can help me put the above query to something I'm more used to? (Example below)
(This query has nothing to do with previous query, it just shows how I'm used to doing queries and how I can actually understand it)
SELECT
dr.id
,u.username as 'driver'
,sloc.name as 'start'
,scom.name as 'startcompany'
,eloc.name as 'end'
,ecom.name as 'endcompany'
,cargoweight
,dc.name as 'cargo'
,dct.name as 'cargotype'
,time
,cargodamage
,drt.name as 'rating'
,distance
,price
,costs
,screenshot
,status
,price - costs - cargodamage as 'profit'
FROM drive_routes dr
INNER JOIN
users u ON u.id = dr.driver
LEFT JOIN
users hb ON hb.id = dr.handledby
INNER JOIN
drive_locations sloc ON sloc.id = dr.start
INNER JOIN
drive_locations eloc ON eloc.id = dr.end
INNER JOIN
drive_companies scom ON scom.id = dr.startcompany
INNER JOIN
drive_companies ecom ON ecom.id = dr.endcompany
INNER JOIN
drive_cargo dc ON dc.id = dr.cargo
INNER JOIN
drive_cargotype dct ON dct.id = dr.cargotype
INNER JOIN
drive_rating drt ON drt.id = dr.rating
WHERE dr.id = ". $_GET['id'] ."
";
try
{
$stmt = $db->prepare($query);
$result = $stmt->execute();
}
catch(PDOException $ex)
{
die("Failed to run query: " . $ex->getMessage());
}
$rows = $stmt->fetchAll();
$count = $stmt->rowcount();
Thanks so much in advance!