简体   繁体   English

如何在SQL查询中设置条件

[英]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在此查询中如何设置条件,如果interchangeNo 为空,则上面是正确的,如果interchangeNo 不为空,则选择interchangeNo 作为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/看看https://www.w3schools.com/sql/func_mysql_if.asphttp://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我很确定这可以改进,但是您缺少的是 if else

You can use JOIN query for connecting multiple tables, there is lot of examples for learning that.您可以使用JOIN查询连接多个表,有很多示例可供学习。

Your query will be like below if you have written query using JOIN :如果您使用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.学习JOIN查询的有用链接

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 .请在interchangeno_is_nullinterchangeno_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). CASE语句遍历条件并在满足第一个条件时返回一个值(如IF-THEN-ELSE语句)。 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.如果没有条件为真,则返回ELSE子句中的值。

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.学习CASE语句的有用链接

You can also use IFNULL() .您也可以使用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如上所述,始终使用现代的、显式的JOIN语法,如我的查询所示

It's best to use the JOIN syntax.最好使用 JOIN 语法。
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.然后你可以使用LEFT JOIN并且一个CASE允许一些逻辑。

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:使用COALESCE() (或其 MySQL 版本IFNULL() ),如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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