简体   繁体   中英

SQL - How do I exclude results from a table if they have the same data in two columns

For example, I have a Cargo Shipment table and within that table there are two columns; Origin and Destination. How would I make it so that any results I get from my select statement will be excluded if the values in these columns are the same?

To be more specific, I could have a row where both Origin and Destination equals 'Chicago', how would I exclude that row without also excluding rows that have either Origin or Destination as Chicago.

SELECT
    *
FROM
    Cargo
WHERE
    Origin != Destination

You can use the WHERE clause to exclude all rows where the origin and destination both equal a specific value using

SELECT *
  FROM SHIPMENTS
 WHERE ORIGIN <> 'val'
       OR DESTINATION <> 'val'

or, if you wanted to exclude all items where the origin and destination are in a list of values, you could use

SELECT *
  FROM SHIPMENTS
 WHERE ORIGIN <> DESTINATION
       OR ORIGIN NOT IN ('list', 'of', 'vals')

If you just need to compare the two columns use the not equal operator =! or <> in your where clause, depending on your needs.

select... where Origin != Destination

Hope it helps.

Here you have a post about this operators Not equal <> != operator on NULL

Try this: for the change requirement

SELECT *
FROM SHIPMENTS
WHERE ORIGIN <> 'Chicago' AND DESTINATION <> 'Chicago'

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