Lori
Lori

Reputation: 59

MySQL using WHERE clause with multiple LIKEs

I have a simple table with names, some beginning with a letter and some beginning with a number. I am trying to filter out the names that start with a number, and only select the names that start with a letter of the alphabet.

Here are a few of my attempts, which have failed to produce the desired results:

SELECT name 
  from subjects 
 WHERE name LIKE ('A%') AND 
 WHERE name LIKE ('B%') AND 
 WHERE name LIKE ('C%')...;

SELECT name 
  from subjects 
 WHERE name LIKE ('A%', 'B%', 'C%', ...);

SELECT name 
  from subjects 
 WHERE name LIKE ('A%') AND ('B%') AND ('C%') ...;

Thank you, in advance, for your help. I appreciate it very much. Stack Overflow has been my savior so many times as I am learning to program with PHP and MySQL. Lori

Upvotes: 0

Views: 104

Answers (3)

Przemysław Kalita
Przemysław Kalita

Reputation: 2017

Try Regexp:

SELECT name 
FROM subjects 
WHERE name
REGEXP '[A-Za-z]'

Upvotes: 0

iamkrillin
iamkrillin

Reputation: 6876

the problem you are having is you have included multiple wheres. Simply remove them

SELECT name 
  from subjects 
 WHERE name LIKE ('A%') AND 
 name LIKE ('B%') AND 
 name LIKE ('C%')...;

Upvotes: 0

vee
vee

Reputation: 38645

A simpler solution would be use regexp operator instead of passing multiple likes.

Try:

SELECT name from subjects WHERE name REGEXP '^[a-zA-Z]';

This will select all records with name starting with both lowercase and uppercase alphabets.

Upvotes: 1

Related Questions