我正在尝试以下列方式选择一些数据:
SELECT column
FROM table
WHERE a = a1
AND (b = b1 OR b = b2 OR b = b3);
我想要它做的是如果 b 不等于 b1,请检查 b = b2。但是,如果 b=b1,则不要检查其他条件。此 select 语句的结果必须只有一个条目。但是,在我没有的语句中,它会检查所有三个条件,有时会返回多行。同样,我希望它停止检查条件是否为真。
关于如何实施的任何想法?我试过case,但没有成功...
先感谢您!
编辑这是我正在尝试运行的实际查询。
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;
这是一个示例输出:
airlinename flt_no flt_date destination phone pcs weight
Everts Air Alaska CH1 2008-02-20 Hughes 9074502351 24 2121
该查询将一堆航班数据插入到临时表中。我遇到的问题是获取某个位置的电话号码。这部分内容如下:
(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
在 Amit Bhargava 建议的查询中,只有在临时表中有一行时,我才会得到正确的结果。如果还有更多,则会在选择电话部分时引发错误。
“错误代码:1242。子查询返回多于 1 行”