我们如何在具有多列的case语句中使用别名?

时间:2016-05-23 10:56:46

标签: sql oracle plsql

我正在尝试使用case语句在表的两列之间切换。 这是我的代码:

<?php
//open connection to mysql db
require "connection.php";

$ItemId=$_POST["ItemId"];
$CustID=$_POST["CustID"];
$restName=$_POST["restName"];

//get rest ID
$sql1 ="SELECT RestID FROM Restaurant WHERE RestName ='$restName'" ;
$result1 = mysqli_query($conn, $sql1) or die("Error in Selecting " . mysqli_error($conn));
$row1 =mysqli_fetch_assoc($result1);
$restID = $row1['RestID'];

//check if item is in list
   $sql2= "SELECT Quantity FROM `Order` WHERE RestID='$restID' AND CustID='$CustID' AND ItemID='$ItemId' AND Received='0'";

   $query2= mysqli_query($conn,$sql2);
   $getRes=mysqli_fetch_assoc($query2);
      $quantity=$getRes["Quantity"];

   if(!$query2){
      $sql ="INSERT INTO `a6933278_InsMeal`.`Order` (`OrderID`, `CustID`, `ItemID`, `RestID`, `Received`, `Quantity`) VALUES (NULL,'$CustID', '$ItemId','$restID','0','1')" ;
       $result = mysqli_query($conn, $sql) or die("Error in Selecting " . mysqli_error($conn));
       echo " has been added";
      }

     else{
       $sql3 ="UPDATE Order SET Quantity= Quantity + 1 WHERE CustID='$CustID' AND  ItemID='$ItemId' AND RestID='$restID' AND Received='0'" ;
       $result = mysqli_query($conn, $sql3) or die("Error in Selecting " . mysqli_error($conn));
       echo " has been added";
    }



//close the db connection
mysqli_close($conn);

如果货币标志为Y,我需要考虑trxn_rptng_am,如果货币标志是N,我需要考虑trxn_base_am。

有人可以帮我解决这个问题。

1 个答案:

答案 0 :(得分:1)

您需要将两个表达式合并为一个:

CASE :currency_flag
    WHEN 'Y' THEN tr.TRXN_RPTNG_AM
    WHEN 'N' THEN tr.TRXN_BASE_AM
    ELSE 0
END AS TRXN_SWITCHED