Erik Åsland
Erik Åsland

Reputation: 9802

mySQL: pattern matching 'one condition or the other but not both'

I am trying to craft code that "Shows the countries that are big by area or big by population but not both". It should show the name, population and area.

The table the code references...

Table the code references

This is my code so far...

SELECT name, population, area FROM world
WHERE area > 3000000 OR population > 250000000 OR name != LIKE '%United States%'


world contains name, area, and population.

Anyone have any advice?

Upvotes: 3

Views: 609

Answers (2)

Barmar
Barmar

Reputation: 782785

You can use XOR for this. It's true if only one of its parameters is true.

SELECT name, population, area
FROM world
WHERE (area > 3000000 XOR population > 250000000) 
    AND name NOT LIKE '%United States%'

I also changed the way the United States test is combined. I assume you're trying to exclude United States from the results, so it needs to be AND.

Upvotes: 3

Jordan Running
Jordan Running

Reputation: 106147

Use the XOR (exclusive OR) operator:

SELECT name, population, area FROM world
WHERE area > 3000000 XOR population > 250000000

Upvotes: 2

Related Questions