samedi 10 juin 2017

Getting the value if the result is null with mysql query - PHP

I have a php code and a mysql query that will get the value if my query is NULL. However, I want it to return only in one ID. The problem is that the code will populate the whole row of the table. Can you give me hints on how to get only the row where there is a null value? Thank you.

You can ignore the SQL queries if you want.... I just want to know how to get only the ID where there is a null value. My query is messed up so yeah..

Here's my code and query:

 $server = "localhost";
      $username = "root";
      $pass = "";
      $dbname = "sample1";

      $conn = new PDO("mysql:host=$server;dbname=$dbname", $username, $pass);
      $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);


  $sqlreport = $conn->prepare(**query for getting all the values if there 
           are no nulls**);

   $sqlreportnull = $conn->prepare("SELECT EMPLOYEE, a.AMOUNT as DECLARED, 
(SELECT coalesce(sum(case when gndsale.type = 5 then gndsale.amount end), 0) 
- coalesce(sum(case when gndsale.TYPEID = 10 then gndsale.amount end), 0)  
from gndsale where gndsale.EMPLOYEE = b.ID and gndsale.DOB = a.DOB  group by 
gndsale.EMPLOYEE having sum(gndsale.type = 5) > 0 and sum(gndsale.TYPEID = 
10) > 0)  as `test`
FROM gndsale a 
INNER JOIN emp b ON a.EMPLOYEE = b.ID  WHERE a.type = 22 and 
STR_TO_DATE(a.DOB, '%m/%d/%Y') BETWEEN '2017-05-01' AND '2017-05-31' and 
(SELECT coalesce(sum(case when gndsale.type = 5 then gndsale.amount end), 0) 
- coalesce(sum(case when gndsale.TYPEID = 10 then gndsale.amount end), 0)  
from gndsale where gndsale.EMPLOYEE = b.ID and gndsale.DOB = a.DOB  group by 
gndsale.EMPLOYEE having sum(gndsale.type = 5) > 0 and sum(gndsale.TYPEID = 
10) > 0) IS NULL

GROUP BY DECLARED order by STR_TO_DATE(a.DOB, '%m/%d/%Y')");

   $sqlreportnotnull = $conn->prepare("SELECT a.DOB as `DATE`, 
concat(b.FIRSTNAME, ' ', b.LASTNAME) Fullname, a.Amount as DECLARED, (SELECT 
sum(gndsale.amount) from gndsale where gndsale.EMPLOYEE = b.ID and 
gndsale.type = 5 and gndsale.DOB = a.DOB) as `other1` 
FROM gndsale a 
  INNER JOIN emp b ON a.EMPLOYEE = b.ID  
INNER JOIN adjtime f on a.EMPLOYEE = f.EMPLOYEE
WHERE a.type = 22 and STR_TO_DATE(a.DOB, '%m/%d/%Y') BETWEEN '2017-05-01' 
AND '2017-05-31' and (SELECT coalesce(sum(case when gndsale.type = 5 then 
gndsale.amount end), 0) - coalesce(sum(case when gndsale.TYPEID = 10 then 
gndsale.amount end), 0)  from gndsale where gndsale.EMPLOYEE = b.ID and  
gndsale.DOB = a.DOB  group by gndsale.EMPLOYEE having sum(gndsale.type = 5) 
> 0 and sum(gndsale.TYPEID = 10) > 0) IS NULL
 GROUP BY DECLARED"");

      $sqlreportnull->execute();
      $sqlreportnotnull->execute();
       $sqlreport->execute();
      $rowsqlreport = $sqlreport->fetchObject();
      $rowsqlreportnull = $sqlreportnull->fetchObject();
      $rowsqlreportnotnull = $sqlreportnotnull->fetchObject();


   <tr> 
     <td>Bulk/Wholesale</td>
                     <td>Others</td>
                     <td><?php echo $rowsqlreport->MetroDeal; ?></td>
    <td>
                         <?php  

                if ($rowsqlreportnull->test == NULL  ){

                         echo $rowsqlreportnotnull->value; 

                     }


                     else {

                     echo $rowsqlreport->OTHERREPORT;
                     }

                     ?>

                     </td>

Aucun commentaire:

Enregistrer un commentaire