MYSQL query search refine issue - php

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

Related

How to search the multiple keywords in multiple columns? [duplicate]

I'm trying to make a search feature that will search multiple columns to find a keyword based match. This query:
SELECT title FROM pages LIKE %$query%;
works only for searching one column, I noticed separating column names with commas results in an error. So is it possible to search multiple columns in mysql?
If it is just for searching then you may be able to use CONCATENATE_WS.
This would allow wild card searching.
There may be performance issues depending on the size of the table.
SELECT *
FROM pages
WHERE CONCAT_WS('', column1, column2, column3) LIKE '%keyword%'
You can use the AND or OR operators, depending on what you want the search to return.
SELECT title FROM pages WHERE my_col LIKE %$param1% AND another_col LIKE %$param2%;
Both clauses have to match for a record to be returned. Alternatively:
SELECT title FROM pages WHERE my_col LIKE %$param1% OR another_col LIKE %$param2%;
If either clause matches then the record will be returned.
For more about what you can do with MySQL SELECT queries, try the documentation.
If your table is MyISAM:
SELECT *
FROM pages
WHERE MATCH(title, content) AGAINST ('keyword' IN BOOLEAN MODE)
This will be much faster if you create a FULLTEXT index on your columns:
CREATE FULLTEXT INDEX fx_pages_title_content ON pages (title, content)
, but will work even without the index.
1)
select *
from employee em
where CONCAT(em.firstname, ' ', em.lastname) like '%parth pa%';
2)
select *
from employee em
where CONCAT_ws('-', em.firstname, em.lastname) like '%parth-pa%';
First is usefull when we have data like : 'firstname lastname'.
e.g
parth patel
parth p
patel parth
Second is usefull when we have data like : 'firstname-lastname'. In it you can also use special characters.
e.g
parth-patel
parth_p
patel#parth
Here is a query which you can use to search for anything in from your database as a search result ,
SELECT * FROM tbl_customer
WHERE CustomerName LIKE '%".$search."%'
OR Address LIKE '%".$search."%'
OR City LIKE '%".$search."%'
OR PostalCode LIKE '%".$search."%'
OR Country LIKE '%".$search."%'
Using this code will help you search in for multiple columns easily
SELECT * FROM persons WHERE (`LastName` LIKE 'r%') OR (`FirstName` LIKE 'a%');
Please try with above query.

Php, Mysql Match in Set multiple values

How can I perform this type of search?
Here is my table:
Name Category
---------------------------------
Company1 food,services
Company2 garden,gifts,services
Company3 other,auto
and here is the query:
$search = $_GET['search'];
"SELECT * FROM companies WHERE category IN ('$search')";
The above search variable looks like mydomain.com?search=garden,services
The idea is to get all relevant rows if any of the matching keywords are present within Category field.
The simplest SQL search is probably a LIKE search:
SELECT * from companies where category LIKE ("%$search%");
You can also do a REGEXP search:
SELECT * from companies where category REGEXP ("$search");
What many have ended up doing, though, is fulltext searching. You can configure MySQL to do this, or perhaps you'll want to try a solution like SphinxSearch.

How to select rows from a table where a word appears? (MySQL + PHP)

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;

Searching database mysql

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'

sql query to like some texts

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

Categories