puretppc
puretppc

Reputation: 3292

How do I exclude a specific record in SQL?

I want to exclude a record in my table but it is not working logically. Here is my Weapons table:

Name / DPS / Style

Noxious Bow / 1000 / Range
Chaotic Staff / 800 / Magic
Armadyl Crossbow / 750 / Range
Saradomin Godsword / 600 / Melee
Dragon Longsword / 600 / Magic
Dragon Longsword / 550 / Melee
Elder Shortbow / 500 / Range

What I'm trying to do is exclude the record Dragon Longsword which has a Style of Melee.

Here is what I tried:

SELECT *
FROM Weapons
Where Name!='Dragon Longsword' AND Style!='Melee';

What is happening is that it is not displaying any record that contains Dragon Longsword or Melee. I want it to not display only the following record:

Dragon Longsword / 550 / Melee

Upvotes: 4

Views: 93

Answers (2)

sstan
sstan

Reputation: 36473

Logically, what you want is:

not (name == 'Dragon Longsword' and Style == 'Melee')

Applying simple boolean logic (De Morgan's laws, thank you @Rocket), this can be translated into:

name != 'Dragon Longsword' or Style != 'Melee'

So the correct query would need an OR, not an AND, as unintuitive as it may seem:

SELECT *
FROM Weapons
Where Name!='Dragon Longsword' OR Style!='Melee';

Upvotes: 9

Vivek Viswanathan
Vivek Viswanathan

Reputation: 1963

SELECT * 
FROM Weapons 
Where Name!='Dragon Longsword' OR Style!='Melee'

Upvotes: 2

Related Questions