mysql query not like several text matches - mysql

I am trying to do a simple query that has a where clause stating there is no match for 2 items:
where l.country not like \"%USA%\" or \"%CA%\" ORDER BY l.state
I also tried:
where l.country not like \"%USA%\" or l.country not like \"%CA%\" ORDER BY l.state
also tried:
where l.country not like (\"%USA%\", \"%CA%\") ORDER BY l.state
is there a way to use "not like" with more than one match?

This is your original condition:
where l.country not like "%USA%" or "%CA%" ORDER BY l.state
I assume you intend this to mean "the country is neither the USA nor CA."
If so, you would write it this way:
where l.country not like '%USA%' and l.country not like '%CA%' ORDER BY l.state
But there's no syntax in SQL for NOT LIKE 'X' OR 'Y'. The LIKE predicate has a left operand and a single right operand, no more.
The expression you wrote is a valid expression, but doesn't do what you think it does. It's like as if you had written this:
where (l.country not like "%USA%") or ("%CA%") ORDER BY l.state
That is, two terms, separated by OR, the first is a LIKE comparison, and the second is just a single string literal on its own. That's a valid term in an expression, but it doesn't do anything useful. It's like writing:
x = 6 * 8 + 0
What effect does the zero have in that expression? None.
Update: I was mistaken, I overlooked one effect of the query as you wrote it. You should know that in a boolean expression if you OR two terms, it doesn't matter what the first term is if the second term is always TRUE.
WHERE (some expression) OR (TRUE)
This is always true.
The literal string '%CA%' counts as true, because it's not an empty string or a NULL. So in your original query, the WHERE clause is always true no matter what the country is.

You could use REGEXP with an alternation:
SELECT *
FROM yourTable
WHERE country NOT REGEXP '"%USA%"|"%CA%"'
Notes:
You don't need to escape double quotes which appear inside of string literals in single quotes. Your original query would not run, I think, because you need to compare a column using LIKE against either another column or a string literal, normally in single quotes.
REGEXP is not case sensitive, so we could have used usa and ca for the same result, though this does not appear to matter in your case.

Related

MySQL command to get first letter of last name

Hello I have made a dummy table that I am practicing with and I am trying to get the lasts name first letter for example. Aba Kadabra and Alfa Kadabra the last letter of their last name is 'K' so when I was testing some queries such as...
select * from employees
where full_name like 'K%'
select * from employees
where full_name like 'K%'
Neither of these worked. Can anyone tell me the best way to accomplish this?
Because % works that way. See here
So, 'K%' just brings all full_name that start with K.
and '%K' brings all full_name that end with K.
What you need is '% K%', test it please.
MySQL LIKE operator checks whether a specific character string matches
a specified pattern.
The LIKE operator does a pattern matching comparison. The operand to
the right of the LIKE operator contains the pattern and the left hand
operand contains the string to match against the pattern. A percent
symbol ("%") in the LIKE pattern matches any sequence of zero or more
characters in the string. An underscore ("_") in the LIKE pattern
matches any single character in the string. Any other character
matches itself or its lower/upper case equivalent (i.e.
case-insensitive matching). (A bug: SQLite only understands
upper/lower case for ASCII characters by default. The LIKE operator is
case sensitive by default for unicode characters that are beyond the
ASCII range. For example, the expression 'a' LIKE 'A' is TRUE but 'æ'
LIKE 'Æ' is FALSE.)
You can use below query:
select * from table where full_name like '% K%'

Why is the regular expression setminus operator not working for MySQL?

I am trying to select all client names without vowels from a table (should therefore return an empty list) using the setminus operator with regular expressions, but it is simply returning the entire column. The same happens if I try to select all client names without 'a' or 'e' or any other vowel.
This is the query I'm using:
select client_name from client
where client_name regexp '[^aeiou]';
If I try doing a condition like below, then the inside caret actually does take every character other than 'a'. I'm not sure why it doesn't work by itself though.
select client_name from client
where client_name regexp '^[^a]'
Expected - empty output
Actual Results - whole column is returned
The regular expression can match anywhere in the name. So it will match any name that has any non-vowel character, not where all the characters are not vowels. You need to anchor it and quantify it:
WHERE client_name REGEXP '^[^aeiou]*$'
This tests all the characters in the name.
Or you can negate the test:
WHERE client_name NOT REGEXP '[aeiou]'
The regexp matches a vowel anywhere in the name. Then using NOT makes this return the names that don't match.

Can anyone tell me in mysql How to display employee names whose name DO NOT start with alphabet A?

I am a beginner so please help me.
There are 2 things you need to combine in this case.
Because you didn't provide enough information in your question we have to guess what you mean by name. I'm going to assume that you have a single name column, but that would be unusual.
With strings, to match a character column that is not an exact match, you need to use LIKE which allows for wildcards.
You also need to negate the match, or in other words show things that are NOT (something).
First to match names that START with 'A'.
SELECT * FROM table_name WHERE name LIKE 'A%';
This should get you all the PEOPLE who have names that "Start with A".
Some databases are case sensitive. I'm not going to deal with that issue. If you were using MySQL that is not an issue. Case sensitivity is not universal. In some RDBMS like Oracle you have to take some steps to deal with mixed case in a column.
Now to deal with what you actually want, which is NOT (starting with A).
SELECT * FROM table_name WHERE name NOT LIKE 'A%';
your question should have more detail however you can use the substr function
SELECT name FROM yourtable
WHERE SUBSTR(name,1,1) <> 'A'
complete list of mysql string functions here
mysql docs
NOT REGXP operator
MySQL NOT REGXP is used to perform a pattern match of a string expression expr against a pattern pat. The pattern can be an extended regular expression.
Syntax:
expr NOT REGEXP pat
Query:
SELECT * FROM emp_table WHERE emp_name NOT REGEXP '^[a]';
or
SELECT * FROM emp_table WHERE emp_name NOT REGEXP '^a';

selecting values that do not have '_' with mysql

I was looking for a way to exclude values with a '_' in the results set from a mysql database.
Why would the following sql statement return no results?
select questionKey
from labels
where set_id = 674
and questionKey like 'Class%'
and questionKey not like '%_%' ;
which was the first sql I tried where as
select questionKey
from labels
where set_id = 674
and questionKey like 'Class%'
and locate('_',questionKey) = 0 ;
returns
questionKey
ClassA
ClassB
ClassC
ClassD
ClassE
ClassF
ClassG
ClassNPS
ClassDis
which is the result I wanted. Both SQL statements appear to me to be logically equivalent though they are not.
As tadman and PM77 already pointed out, it's a special character. If you want to use the first query, try to escape it like this (note the backslash):
select questionKey
from labels
where set_id = 674
and questionKey like 'Class%'
and questionKey not like '%\_%' ;
In the LIKE context _ takes on special meaning and represents any single character. It's the only one other than % that means something here.
Your LOCATE() version is probably the best here, though it's worth noting that doing table scans like this can get cripplingly slow on large amounts of data. If underscore represents something important you might want to have a flag field you can set and index.
You could also use a regular expression to try and match records with a single condition:
REGEXP '^Class[^_]+'

Mysql SELECT all rows where char exists in value but not the last one

I need a SELECT query in MYSQL that will retrieve all rows in one table witch field values contain "?" char with one condition: the char is not the last character
Example:
ID Field
1 123??see
2 12?
3 45??78??
Returning rows would then be those from ID 1 and 3 that match the condition given
The only statement I have is:
SELECT *
FROM table
WHERE Field LIKE '%?%'
But, the MySQL query does not solve my problem..
The LIKE expressions also support a wildcard "_" which matches exactly one character.
So you can write an expression like the example below, and know that your "?" will not be the last character in the string. There must be at least one more character.
WHERE intrebare LIKE '%?_%'
Re comment from #JohnRuddell,
Yes, that's true, this will match the string "??" because a "?" exists in a position that is not the last character.
It depends whether the OP means for that to be a match or not. The OP says the string "45??78??" is a match, but it's not clear if they would intend that "4578??" to be a match.
An alternative is to use a regular expression, but this is a little more tricky because you have to escape a literal "?", so it won't be interpreted as a regexp metacharacter. Then also escape the escape character.
WHERE intrebare REGEXP '\\?[^?]'
you can just add an additional where where the last character is not a ?
SELECT *
FROM intrebari
WHERE intrebare LIKE '%?%' AND intrebare NOT LIKE '%?'
you could also do it like this
SELECT *
FROM intrebari
WHERE intrebare LIKE '%?%' AND RIGHT(intrebare,1) <> '?'
DEMO