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'
Related
I have a table "songs" and column "singers". I want to perform a search on table "songs" by the name of the singer. My problem is, if the name of the singer is "Gallagher" the query gives me not only the singer "Gallagher" but also all other names with "Gallagher" in it, for example "Noel Gallagher" "Liam Gallagher", etc...
How can I refine my search to only give me "Gallagher" if the search string is "Gallagher"?
My query is this:
SELECT * FROM songs WHERE singers RLIKE \"" . $searchstring . "\" ORDER BY title
Edit: After searching stackoverflow, here is a solution you maybe looking for: MySQL - How to search for exact word match using LIKE?
What‘s you‘r searchquery exactly?
SELECT * FROM songs WHERE singers RLIKE '^Gallagher';
If you want to display 'Gallagher only then use SUBSTRING_INDEX
SELECT DISTINCT SUBSTRING_INDEX(singers, " ", -1) FROM songs WHERE singers LIKE '%{$search_string}%' ORDER BY title;
If you want search single name based on last name (surname) then you need to write below query
SELECT * FROM songs WHERE singers LIKE '%Gallagher';
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.
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 am working on an application in which i want to create a search bar which will be able to search for individual users as well as brands.
For this i have written a query like this :
SELECT * FROM temp_users,brand_pages WHERE firstname LIKE'%$find%' or lastname LIKE '%$find%' or name LIKE '%$find%'
I checked the query but it is not as needed. What is the way it can be done ??
Should i use joins ??
Yes you need join.
Try this:
SELECT *
FROM temp_users AS TU
INNER JOIN brand_pages AS BP
WHERE (TU.firstname LIKE "%$find%" OR TU.lastname LIKE "%$find%" OR BP.name LIKE "%$find%")
Edit: If you are using this query in PHP and trying to use the variable $find inline, use double quotes (not single quotes).
here's my table:
and I want get the customers which have some values of for/category fields which is comma separated..
I am trying something like this:
SELECT * FROM `customers` WHERE `for` LIKE ('%AMC PHD & WWS%' OR '%Rostfrei%' OR '%Thermopac%')
but its giving empty result.
RedFilter's SQL is correct, but you should also know that "for" is a MySQL reserved word. You should avoid using it as a column name, or wrap it in backticks when you use it:
SELECT *
FROM customers
WHERE `for` LIKE '%AMC PHD & WWS%'
OR `for` LIKE '%Rostfrei%'
OR `for` LIKE '%Thermopac%';
The alternative, typing the column name once is:
SELECT * FROM customers WHERE `for` REGEXP 'AMC PHD \& WWS|Rostfrei|Thermopac';
Try:
SELECT *
FROM customers
WHERE for LIKE '%AMC PHD & WWS%'
or for LIKE '%Rostfrei%'
or for LIKE '%Thermopac%'