[wd_asp elements=’search’ ratio=’100%’ id=1]

IN statement example with JOIN highest ID

19th March 2014

MySql

mysql codehaven category

This will pull the amount of the record with the highest ID of the campaign called Car.
and will pull the amount of the record with the highest ID of the campaign called Van, and so on in the IN(*) part.
It then adds them altogether, and outputs in $dog

$result = mysql_query("SELECT amount FROM table1
JOIN (
SELECT MAX(id) as id
FROM cmdata
WHERE campaign IN ('Car', 'Van', 'Bike', 'Bus')
GROUP BY campaign) t ON t.id = cmdata.id");

while($row = mysql_fetch_array($result))
{
$sum = $row['amount'];
$dog = $dog + $sum;
}

echo "
There are $dog";