Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 453
puretppc Avatar asked Dec 19 '22 23:12

puretppc


1 Answers

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';
like image 102
sstan Avatar answered Jan 06 '23 02:01

sstan