简体   繁体   English

MySQL存储过程:IF在WHERE中

[英]MySQL stored procedure: IF in WHERE

I am trying to select some data in the following manner: 我试图以下列方式选择一些数据:

SELECT column
FROM table
WHERE a = a1
AND (b = b1 OR b = b2 OR b = b3);

What I want it to do is if b is not equal to b1, check if b=b2. 我想要它做的是如果b不等于b1,检查b = b2。 However, if b=b1, do not check other conditions. 但是,如果b = b1,请不要检查其他条件。 The result of this select statement must be only one entry. 此select语句的结果必须只是一个条目。 However, in the statement I have no, it checks all of the three conditions and sometimes returns multiple rows. 但是,在我没有的语句中,它会检查所有这三个条件,有时会返回多行。 Again, I would like it to stop checking if the condition is true. 同样,我希望它停止检查条件是否为真。

Any ideas on how could this be implemented? 有关如何实施的任何想法? I tried case but it did not work out... 我试过案例,但没有成功...

Thank you in advance! 先感谢您!

EDIT Here is an actual query i am trying to run. 编辑这是我想要运行的实际查询。

INSERT INTO shipment_flights 
    (airlinename, flt_no, flt_date, destination, phone, depttime, arrivaltime, pcs, weight)
SELECT st.airlinename, flightno, flightdate, destination,     
(SELECT phone 
FROM carrierlocations 
WHERE carriers_carrierid = (select carrierid from carriers where airlinename = st.airlinename) 
AND (city = destination OR (city != destination AND
    city = (SELECT city FROM airports WHERE iataid = 
            (SELECT airports_iataid FROM ratelegs 
                WHERE shipments_shipid = c.shipments_shipid))
                ))) phone, 
depttime, arrivaltime, sum(linepcs), sum(lineweight)
FROM segment_times st
    JOIN contents2flights c2f 
        ON st.flightid = c2f.segments_flights_flightid 
        AND st.segmentid = c2f.segments_segmentid 
    JOIN contents c 
        ON c.lineno = c2f.contents_lineno 
        AND c.shipments_shipid = c2f.contents_shipments_shipid

WHERE c.shipments_shipid = var_shipid
GROUP BY flightid
ORDER BY flightdate, depttime;

Here is a sample output: 这是一个示例输出:

airlinename         flt_no  flt_date        destination    phone         pcs   weight
Everts Air Alaska   CH1     2008-02-20      Hughes         9074502351    24    2121

The query inserts bunch of flight data into temporary table. 查询将一堆飞行数据插入临时表。 What I am having trouble with is getting a phone number for a location. 我遇到的问题是获取某个地点的电话号码。 This part is as follows: 这部分内容如下:

(SELECT phone 
FROM carrierlocations 
WHERE carriers_carrierid = (select carrierid from carriers where airlinename = st.airlinename) 
AND (city = destination OR (city != destination AND
    city = (SELECT city FROM airports WHERE iataid = 
            (SELECT airports_iataid FROM ratelegs 
                WHERE shipments_shipid = c.shipments_shipid))))) phone

In the query advised by Amit Bhargava, I get the right result only if there is one row in the temporary table. 在Amit Bhargava建议的查询中,只有临时表中有一行时才会得到正确的结果。 If there are more, it throws an error in the selecting phone part. 如果还有更多,则会在选择手机部分时抛出错误。

"Error Code: 1242. Subquery returns more than 1 row" “错误代码:1242。子查询返回超过1行”

By using IF() + IF() + IF(), and testing the sum = 1 prevents a lot of extra and not of the other criteria. 通过使用IF()+ IF()+ IF(),并测试sum = 1可以防止很多额外的而不是其他标准。 If 2 or all 3 are the same, the summation will be greater than 1. If none of them match, the result is 0. This should get exactly what you want. 如果2或全部3相同,则总和将大于1.如果它们都不匹配,则结果为0.这应该完全符合您的要求。

SELECT column
  FROM table
  WHERE a = a1
    AND  if( b = b1, 1, 0 )
       + if( b = b2, 1, 0 )
       + if( b = b3, 1, 0 ) = 1

Or... as assisted by ypercube, and I keep forgetting logical tests return either 1 or 0 for true / false respectively, such as... 或者......在ypercube的帮助下,我一直忘记逻辑测试分别返回1或0表示真/假,例如......

SELECT column
  FROM table
  WHERE a = a1
    AND  (b = b1) + (b = b2) + (b = b3) = 1

Please try the following. 请尝试以下方法。 Not the most elegant solution, but it should work. 不是最优雅的解决方案,但它应该工作。

SELECT column
FROM table
WHERE a = a1
AND (b = b1 OR (b != b1 AND (b = b2 OR (b != b2 AND b = b3))))

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

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