You can give this a try. I think it's probably a little more verbose than it needs to be, but should do what you are asking. Not going to guarantee it is what you want, so run it through some tests first
<?php
//Var to hold result
$result = false;
//Query for an exact match on both gpw and ewt
$query = "SELECT gpm, ewt, totCool from {$uniluxModel} where gpm = :uniluxGpm AND ewt = :uniluxEwt";
$stmt = $dbConn->prepare($queryStr);
$stmt->execute(array(':uniluxGpm' => $uniluxGpm, ':uniluxEwt' => $uniluxEwt));
if($stmt->rowCount() > 0)
{
//There was an exact match
$result = $stmt->fetchColumn()
}
//If no result try getting the two closest records matching gpm and high/low for ewt
if (!$result)
{
$query = "(
SELECT gpm, ewt, totCool
FROM wxyz
WHERE gpm = :uniluxGpm1 AND ewt < :uniluxEwt1
ORDER BY gpm DESC
LIMIT 1
)
UNION
(
SELECT gpm, ewt, totCool
FROM wxyz
WHERE gpm = :uniluxGpm2 AND ewt > :uniluxEwt2
ORDER BY gpm ASC
LIMIT 1
)";
$stmt = $dbConn->prepare($queryStr);
$stmt->execute(array(':uniluxGpm1' => $uniluxGpm, ':uniluxEwt1' => $uniluxEwt, ':uniluxGpm2' => $uniluxGpm, ':uniluxEwt2' => $uniluxEwt));
if($stmt->rowCount() > 0)
{
//There was a result
$result = $stmt->fetchColumn()
}
}
//If no result try getting the two closes records matching ewt and high/low for gpm
if (!$result)
{
$query = "(
SELECT gpm, ewt, totCool
FROM wxyz
WHERE gpm < :uniluxGpm1 AND ewt = :uniluxEwt1
ORDER BY gpm DESC
LIMIT 1
)
UNION
(
SELECT gpm, ewt, totCool
FROM wxyz
WHERE gpm > :uniluxGpm2 AND ewt = :uniluxEwt2
ORDER BY gpm ASC
LIMIT 1
)";
$stmt = $dbConn->prepare($queryStr);
$stmt->execute(array(':uniluxGpm1' => $uniluxGpm, ':uniluxEwt1' => $uniluxEwt, ':uniluxGpm2' => $uniluxGpm, ':uniluxEwt2' => $uniluxEwt));
if($stmt->rowCount() > 0)
{
//There was a result
$result = $stmt->fetchColumn()
}
}
//If no result get the two closest records that are both above gpm and ewt and both below gpm and ewt
if (!$result)
{
$query = "(
SELECT gpm, ewt, totCool
FROM wxyz
WHERE gpm < :uniluxGpm1 AND ewt < :uniluxEwt1
ORDER BY gpm DESC, ewt DESC
LIMIT 1
)
UNION
(
SELECT gpm, ewt, totCool
FROM wxyz
WHERE gpm > :uniluxGpm2 AND ewt > :uniluxEwt2
ORDER BY gpm ASC, ewt ASC
LIMIT 1
)";
$stmt = $dbConn->prepare($queryStr);
$stmt->execute(array(':uniluxGpm1' => $uniluxGpm, ':uniluxEwt1' => $uniluxEwt, ':uniluxGpm2' => $uniluxGpm, ':uniluxEwt2' => $uniluxEwt));
if($stmt->rowCount() > 0)
{
//There was a result
$result = $stmt->fetchColumn()
}
}
if(!$result)
{
//No records that match either value
echo "Not enough data to compute";
}
else
{
//Do something with the data
}
?>