MySQL query LIKE getting error - php

I want to search for people whose name start with letter A using LIKE query but I'm getting errors. What query should I type in order to get results based on people whose name start with a particular letter or not with that letter?

Get result where name starts with a specific letter
SELECT * FROM table WHERE name LIKE 'A%'
Get result where name DOES NOT start with a specific letter
SELECT * FROM table WHERE name NOT LIKE 'A%'

Related

Select results that contains part of string mysql php

I'm looking for a method to get rows that are more similar with inserted string
For example:
SELECT * FROM db WHERE column have part of '$search_string';
In other words if search string is My name is TOM and in the column I have
COLUMN
---------------
My name is Sara
My name is Jack
My dog is white
---------------
results must be:
all rows that contains My name is, because they are most similar to $search_string.
I've tried to use LIKE operator with phrase splatted in words, but I'm not obtaining the result that I want, any ideas?
What about
SELECT * FROM db WHERE COLUMN LIKE 'My name is %'
?
UPDATED
"SELECT * FROM db WHERE COLUMN LIKE '%$search%'"

Internal site search is matching exact word only

I am making an eCommerce website I am using PHP and Mysql and I have some products name in table named product. How can I make a search system by which if I type "Wallnuts", it should return all results having word "wallnut, walnut, walnuts" and word with same pronunciation like "valnuts" and sorted as best matching result first. For this I am trying this by this query :
select product_name,photo,in_stock,sell_price
from $tbl_product
where product_name like '%".$q."%'
ORDER BY (CASE WHEN product_name LIKE '".$q."%' THEN 1
WHEN product_name LIKE '%".$q."%' THEN 2 ELSE 3
END) limit 0,10
Where $q is search string. By this query I am getting result but with only exact word match.
I need result like on Bigbasket.com for word wallnuts, wallnut, walnts, valnuts.
The closest you can try is SOUNDEX() function in MYSQL.
select * from mytext
where soundex(val) LIKE CONCAT('%',SOUNDEX('wallnut'),'%')
This gives me wallnuts, wallnut, walnts.Check Demo here

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.

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
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]'

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