简体   繁体   中英

How to select rows from table that matches where clause on two columns?

How to select rows from table that matches where clause on two columns? I came up with below query. Please advice if there is better way with less text.

SELECT name, number 
FROM namenumber 
WHERE name IN (“Car”) AND number = “1234"
UNION 
SELECT name, number 
FROM namenumber 
WHERE name IN ("Train") AND number = "5678"
UNION 
SELECT name, number 
FROM namenumber 
WHERE name IN ("Flight") AND number = "9012";
SELECT name, number FROM namenumber 
WHERE (name, number) IN ( 
    (“Car”,“1234"), ("Train", "5678"), ("Flight", "9012") 
) ;

Could be just like this this

    SELECT name, number FROM namenumber 
         WHERE (name in (“Car”) and number = “1234") OR 
               (name in ("Train") and number = "5678") OR 
               (name in ("Flight") and number = "9012")

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