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.
Related
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'
I'm trying to select from a table where a column which has a string in it matches some criteria.
the normal way without the string scenario is like this:
SELECT * FROM tablename HWERE columnName='something'
but lets say the columnName contains a value like this:
1,2,3,4 | someemail#yahoo.com
and we want to select from the table where the columnName contains the someemail#yahoo.com. So, how would i need to go about this?
I tried something like this but I'm 100% sure I'm doing it wrong:
SELECT SUBSTRING_INDEX(columnName,' | ',-1) from tableName
because I don't see how the WHERE clause come in that statement!
Could someone please advise on this issue?
You need to use LIKE
SELECT * FROM tablename WHERE columnName LIKE '%someemail#yahoo.com%'
Use LIKE
SELECT * FROM tablename WHERE columnName LIKE '%someemail#yahoo.com%';
I have to make a search for keywords as part of my Computer Science work.
I have the names and descriptions of several DVD's.
The user has to search a word, and then displayed are all the names of DVD's where the word appeared in either the title or description.
Let's say my columns in my table
were "dvd title" and "description", and the word the person has entered is $keyword.
How would i select rows in mysql where $keyword appears at least once in either columns "dvd title" and "description".
Thanks for reading. Any help appreciated.
You could create a full text index on those columns, but that probably isn't what they want you do do.
You need wildcards, and to you wildcards compare with the keyword LIKE instead of =. A wildcard in mysql is %
SELECT * FROM mutable WHERE dvdtitle like '%keyword%' or description like '%keyword%';
As for using PHP variable and creating the string, you've got to do some of your own homework.
$sql = "SELECT * FROM table_name WHERE dvdtitle LIKE '%".$keyword."%' OR description LIKE '%".$keyword."%'";
Executing the above SQL query would return all the rows in the table that has the specified keyword in either the column dvdtitle or description.
Use this query:
SELECT *
FROM table_name
WHERE dvd_title like '%$keyword%'
OR description like '%$keyword%'
You can use following code sample, it's not a full code but will give you an idea:
$query_str = "select * from dvd_table_name where dvd_title like '%$keyword%' or description like '%$keyword%'";
$qh = mysql_query($query_str);
Then use mysql_fetch_assoc($qh) to retrive data using while loop;
I created a stored procedure to search my database by name. I have many text files saved in database named like: a.txt, a1.txt,a2.txt,a3.txt,...
When i run my stoerd procedure only 1 resoult is showing.
SELECT * FROM cars WHERE name LIKE CONCAT ('%', search ,'%');
What is the problem here?
Your problem solution is
SELECT * FROM cars WHERE name LIKE CONCAT ('%'. $search . '%');
or if you are using directly then on sql server then
SELECT * FROM cars WHERE name LIKE CONCAT ('%search%');
If $search (note the dollar sign) is a PHP variable (I assume that because you tagged the question with "php"), you don't have to use the SQL function CONCAT but join the strings in PHP and send it all to MySQL:
$sql = 'SELECT * FROM cars WHERE name LIKE "%'.$search.'%"';
That's because CONCAT will make your % treated as a character, not a search criterion.
try this
SELECT * FROM cars WHERE name LIKE '%$search%' ;
or this
WHERE name LIKE '$search'
My site is like imdb.com - it stores movie details and actor's names. In my actor field, I'm storing actors name like "name1,name2,name3,name4".
My site has the option to search movies by actor name. My code for this is below:
SELECT * FROM imdb WHERE actors LIKE '%%actorname%%'
The problem is that if a visitor searches for 'Lal', then movies with actor's named 'Mohanlal' and 'Lal' will both appear. I only want to list the 'Lal' movies. How can I write the query to do that?
Try the following query:
SELECT * FROM `imdb` WHERE `actors` RLIKE '[[:<:]]Lal[[:>:]]'
Even better, and easier:
SELECT * FROM `imdb` WHERE find_in_set('Lal',actors)
Make the query like this:
SELECT * FROM imdb WHERE actor LIKE 'actorname'
and put a text next to your inputfield to let them use wildcards for search. Then, if they want to search for 'Lal', the will only find 'Lal', if they want to search for everything that ends on 'lal' (%lal), they will find Mohanlal and Lal.
You should have created separated table for actors, anyway we will try to find solution of what we are left to.
SELECT * FROM imdb WHERE actors LIKE 'Lal,%' OR actors LIKE '%,Lal%' OR actors = 'Lal'
SELECT * FROM imdb WHERE actor LIKE '%,lal' or actor LIKE 'lal'
I think this is what you need:
SELECT whatever
FROM whereever AS t
WHERE t.actors = SOUNDEX('test')