简体   繁体   中英

How to set condition in SQL query

SELECT partnumber, p.partdescription, i.invoicenumber, id.quantity, p.interchangeno, p.makemodelname , c.rte
FROM Invoice i,
     invoicedetails id,
     Parts p,
     customer c  
WHERE i.customerid = c.customerid 
  AND i.invoicenumber = id.invoicenumber
  AND i.invoicenumber 
  AND id.partnumber = p.partNo
  AND p.UnitsInStock < 0 AND id.quantity > 0;

In this query how to set condition that if interchangeNo is empty then above is right if interchangeNo is not empty then select interchangeNo as partNo

I guess you need a case statement -

SELECT partnumber, p.partdescription, i.invoicenumber, id.quantity,
       CASE WHEN p.interchangeno IS NULL THEN p.interchangeno
            ELSE partNo END interchangeno, p.makemodelname , c.rte
FROM Invoice i
JOIN invoicedetails id ON i.invoicenumber = id.invoicenumber
JOIN Parts p ON id.partnumber = p.partNo
JOIN customer c ON i.customerid = c.customerid 
WHERE p.UnitsInStock < 0 AND id.quantity > 0;

have a look at https://www.w3schools.com/sql/func_mysql_if.asp and http://www.sqlservertutorial.net/sql-server-stored-procedures/sql-server-if-else/

SELECT  p.interchangeno
FROM Invoice i,
     invoicedetails id,
     Parts p,
     customer c  
WHERE i.customerid = c.customerid 
  AND i.invoicenumber = id.invoicenumber
  AND i.invoicenumber 
  AND id.partnumber = p.partNo
  AND p.UnitsInStock < 0 AND id.quantity > 0;
if (p.interchangeno =0)
SELECT partnumber, p.partdescription, i.invoicenumber, id.quantity, p.interchangeno, p.makemodelname , c.rte
FROM Invoice i,
     invoicedetails id,
     Parts p,
     customer c  
WHERE i.customerid = c.customerid 
  AND i.invoicenumber = id.invoicenumber
  AND i.invoicenumber 
  AND id.partnumber = p.partNo
  AND p.UnitsInStock < 0 AND id.quantity > 0;

else
-- code that you want 

I'm pretty sure that this can be improved, but what you're missing is the if else

You can use JOIN query for connecting multiple tables, there is lot of examples for learning that.

Your query will be like below if you have written query using JOIN :

SELECT `INV_DTL`.`partnumber`, `P`.`partdescription`, `INV`.`invoicenumber`, `INV_DTL`.`quantity`, 
    (CASE WHEN `P`.`interchangeno` IS NULL THEN interchangeno_is_null ELSE interchangeno_is_not_null END) AS interchangeno, `P`.`makemodelname`, `C`.`rte` 
FROM `invoice` INV 
JOIN `invoicedetails` INV_DTL ON (`INV_DTL`.`invoicenumber` = `INV`.`invoicenumber`) 
JOIN `parts` P ON (`P`.`partNo` = `INV_DTL`.`partnumber`) 
JOIN `customer` C ON (`C`.`customerid` = `INV`.`customerid`) 
WHERE `P`.`UnitsInStock` < 0 AND `INV_DTL`.`quantity` > 0;

A helpful link to learn JOIN query.

You only need below query part for filling your issue:

CASE WHEN `P`.`interchangeno` IS NULL THEN interchangeno_is_null ELSE interchangeno_is_not_null END

Please use the required value on interchangeno_is_null & interchangeno_is_not_null .

The CASE statement goes through conditions and returns a value when the first condition is met (like an IF-THEN-ELSE statement). So, once a condition is true, it will stop reading and return the result. If no conditions are true, it returns the value in the ELSE clause.

The syntax is like:

CASE
    WHEN condition1 THEN result1
    WHEN condition2 THEN result2
    WHEN conditionN THEN resultN
    ELSE result
END;

A helpful link to learn CASE statement.

You can also use IFNULL() .

SELECT partnumber, p.partdescription, i.invoicenumber, id.quantity, 
       IFNULL(p.interchangeno,p.partnumber), p.makemodelname , c.rte
FROM Invoice i join invoicedetails id on i.invoicenumber = id.invoicenumber
      join Parts p on .... -- join condition goes here
      join customer c  on ... --join condition goes here
WHERE p.UnitsInStock < 0 AND id.quantity > 0

As mentioned above,always use modern, explicit JOIN syntax as shown in my query

It's best to use the JOIN syntax.
The comma syntax was already outdated by the end of the last century.

And then you can use LEFT JOIN And a CASE allows a bit of logic.

SELECT 
CASE 
WHEN p.partNo IS NULL
THEN id.partnumber
WHEN p.interchangeno IS NOT NULL
THEN id.partnumber 
ELSE p.partNo
END AS partNo,
p.partdescription, 
i.invoicenumber, 
id.quantity, 
p.interchangeno, 
p.makemodelname,
c.rte
FROM Invoice i
INNER JOIN invoicedetails id
  ON id.invoicenumber = i.invoicenumber
INNER JOIN customer c  
  ON c.customerid = i.customerid 
LEFT JOIN Parts p
  ON p.partNo = id.partnumber
 AND p.UnitsInStock < 0
WHERE id.quantity > 0;

Use COALESCE() (or its MySQL version IFNULL() ), something like this:

SELECT partnumber, p.partdescription, i.invoicenumber, id.quantity, p.interchangeno,
       p.makemodelname, c.rte
FROM Invoice i
  JOIN invoicedetails id ON i.invoicenumber = id.invoicenumber
    JOIN Parts p         ON id.partnumber = COALESCE(p.interchangeno, p.partNo)
      JOIN customer c    ON i.customerid = c.customerid
WHERE p.UnitsInStock < 0
  AND id.quantity > 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM