W
W
wolverine7772020-11-05 23:31:29
SQL
wolverine777, 2020-11-05 23:31:29

How to select fields that are missing what you are looking for?

I don't understand why I can't NOT LIKE when I want to select fields that DO NOT PRESENT the keyword.

For example (it's kind of a star wars tutorial db) if i want to select all the episodes in which Yoda is PRESENT - the following works fine.

SELECT DISTINCT Episode
FROM PERSONNAGE as P, COMPOSER as C
WHERE P.CodePers = 'yoda'
AND P.CodePers = C.CodePers

And when I want to choose, on the contrary, the episode in which Yoda is not - for some reason nothing comes out

SELECT DISTINCT Episode
FROM PERSONNAGE as P, COMPOSER as C
WHERE P.CodePers NOT LIKE 'yoda'
AND P.CodePers = C.CodePers


Where is the mistake?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
Nujabes37, 2020-11-05
@Nujabes37

'abc%'	Любые строки, которые начинаются с букв «abc»
'%z'	Любая последовательность символов, которая обязательно заканчивается символом «z»

WHERE P.CodePers NOT LIKE '%yoda%'

R
Rsa97, 2020-11-06
@Rsa97

Error in logic. If the episode contains at least one character other than Yoda, then the condition NOT LIKE 'yoda'or != 'yoda'will be met and the episode will be selected.

V
Vitaly Voloboev, 2020-11-07
@VitaliiVV

I'm not sure about the wording, but you can try without joins with a
WHERE NOT EXISTS check, or you can solve this issue with a table difference (using except)
(Syntax errors are possible, I would play with the base myself if possible))
SELECT DISTINCT Episode
FROM PERSONNAGE as P, COMPOSER as C
WHERE NOT EXISTS
( SELECT DISTINCT Episode
FROM PERSONNAGE
WHERE CodePers = 'yoda')
AND P.CodePers = C.CodePers
SELECT DISTINCT Episode
FROM PERSONNAGE
EXCEPT
SELECT DISTINCT Episode
FROM PERSONNAGE as P, COMPOSER as C
WHERE P.CodePers = 'yoda'
AND P.CodePers = C.CodePers

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question