MySQL using WHERE clause with multiple LIKEs - php

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

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.

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%')...;

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

Related

How to Select a keyword from username or city in single query

How to select data from two columns in same table in a single query.
for example select name "waseem" from username if not found check in city row.
select * from <table_name> where name like 'waseem' or city like 'waseem';
I agree with other's answers but with slight modification. If there is a record waseem ahemed and your only looking for waseem wont be searched in other answered query.
You need to use the wildcard % if you want to search for waseem.
I highly suggest you not to use * for selecting the column names because of the performance issues.
SELECT name, username, password WHERE name LIKE '%waseem%' OR city LIKE '%waseem%'
This is working for me
SELECT * FROM table WHERE row_name1 LIKE 'value' OR row_name2 LIKE 'value'

SELECT * FROM tbl name WHERE name LIKE 'ask'

Hy, i have a sql tabl name kill, with fields like
ID
name
lname.
There are several names are same, like ali, kiran etc, i want to show all the people with the name ali, so i tried this
SELECT * FROM ask WHERE name LIKE 'ali'
but it shows only the last added ali, please will you tell me the right way to do this. thanks
IF you are trying to find all values containing ali for e.g.
Bali
Alison
etc...
What you need to do is run a wildcard search query, so try this:
SELECT * FROM ask WHERE name LIKE '%ali%'
This will find all values where name contains part of ali in it.
If you want to find all names ending in ali, you can do this:
SELECT * FROM ask WHERE name LIKE '%ali'
If you want to find all names starting with ali, you can do this:
SELECT * FROM ask WHERE name LIKE 'ali%'
etc...
I prefer to use REGEXP, for example:
SELECT * FROM ask WHERE name REGEXP 'ali';
Your query should be as below:-
SELECT * FROM ask WHERE name LIKE '%ali%'
Check this link for detailed info.
SELECT * FROM ask WHERE name LIKE '%ali%';
The Syntax for query in your case is
SQL LIKE Syntax
SELECT column_name(s) FROM table_name WHERE column_name LIKE pattern;
You should use query below to selects all names starting with the letter "ali":
SELECT * FROM ask WHERE name LIKE 'ali%';
You should use query below to selects all names ending with the letter "ali":
SELECT * FROM ask WHERE name LIKE '%ali';
You should use query below to selects all names containing the letter "ali":
SELECT * FROM ask WHERE name LIKE '%ali%';
Refer This link for tutorials on Like.

How to check with a value in comma separated values in MySQL?

I have a MySQL problem. This is just an example just to solve my real problem. Let's say I have a table like this:
Columns: id, name, classes.
Classes field is comma separated value like 1,4,5,4
I know database design is wrong and very bad.
What I want to do is select name where class is equal to 4 whereas the classes field has value 3,4,6,3,8 in MySQL. How can I do that?
You could use the FIND_IN_SET function in MySQL
SELECT name
FROM table_name
WHERE FIND_IN_SET('4',classes) > 0
use of find_in_set function like below-
select * from table_name where find_in_set('4',classes);

SORT Database by Parts of a String?

I do have a FIELD called CUSTOMERID in my Database which has values that look like this:
SFFG2443
The last four characters are always a four digit number which counts up, the first four characters are some random letters means it looks like that:
SFFG2443
FGTG2444
XDGG2445
...
I simply want to sort my table by only taking the LAST four digits(2443,2444,2445) into account. Is that possible with a single SQL-Statement? Thank you very much in advance.
SELECT * FROM yourtable
ORDER BY RIGHT(CUSTOMERID,4)
Should do the trick!
The Comment from Nobert van Nobelen works perfectly. The whole query looks like this:
SELECT customerid FROM customers ORDER BY substr(customerid,4,8) DESC LIMIT 0,1
Here is a link to the documentary for substr().

MySQL Search For Names - Separated by Space

I have a MySQL database and a table which has a names column as follows:
id name age
1 samuel lee 31
2 doug sokira 32
3 vernon smith 33
I would like to search through the database for names. I have this query so far:
SELECT * FROM table WHERE name LIKE 's%'
This would return me the first record with ID 1.
However, i would like the query to further do the search from the text separated by the space as well. So, to put in words the search should be done through both first name and last name.
I'm willing to stick to this database design where first name and last name are stored in one single column.
So, the above query should return rows 1, 2 and 3.
How should i formulate my query?
SELECT * From table WHERE name LIKE 's%' OR name LIKE '% s%'
Note that this cannot use any index and therefore will always be dog-slow. Please reconsider splitting the columns.
SELECT * FROM table WHERE name LIKE '%s%'
that would return row 1, 2, and 3 since they all contain an 's'.

Categories