I'm trying to select posts using MATCH to find strings that contain the exact word. Currently it selects strings that contains the word no matter what it looks like. I want it to select strings that contains the exact value as defined. the sql code currently looks like this:
$searchstring = "#cat";
SELECT * FROM posts WHERE MATCH(content) AGAINST ('$searchstring' IN BOOLEAN MODE)
How do I get it to select the strings that contains the exact value that is defined?
You could switch over to using LIKE
$sql="SELECT * FROM `posts` WHERE `content` LIKE '%{$searchstring}%'"
That would find only posts with at least that search string in it.
$sql="SELECT * FROM `posts` WHERE `content` LIKE '%{$searchstring}%'"
But the above query will search and display all the results having $searchstring in it.
could use :
$sql="SELECT * FROM `posts` WHERE `content` LIKE '::{$searchstring}::'"
Related
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.
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;
Hello i am trying to get the like statement working however it will only work if its an exact match where as im am hoping to show all matches. So lets say you search "and" in the search bar, i want it to come back with all posts that have the word and in the title Below is the code
SELECT * FROM `Posts` WHERE title LIKE 'and'
Just trying to get this working in the phpmyadmin with the sql tab before i bother creating the actual full script.
Does anyone know how i can get the above statement to show all posts with and in the title?
Thanks for the help.
You have to put % before and after and.
SELECT * FROM `Posts` WHERE title LIKE '%and%'
Use:
SELECT * FROM `Posts` WHERE title LIKE '% and %'
Doing something like:
SELECT * FROM `Posts` WHERE title LIKE '%and%'
will include anything with and in it; sandy, for example.
try this placing % symbol
SELECT * FROM `Posts` WHERE title LIKE '%and%'
Do like this:
SELECT * FROM `Posts` WHERE title LIKE '%and%'
MySQL provides two wildcard characters for using with the LIKE operator, the percentage % and underscore _.
The percentage ( %) wildcard allows you to match any string of zero or more characters.
The underscore ( _) wildcard allows you to match any single character
Suppose you want to search for posts whose title starts with character ‘ and‘, you can use the percentage wildcard ( %) at the end of the pattern
SELECT * FROM `Posts` WHERE title LIKE '%and%'
If you know the searched string is embedded inside in the column, you can use the percentage ( %) wildcard at the beginning and the end of the pattern
SELECT * FROM `Posts` FROM books WHERE title REGEXP '[[:<:]]and[[:>:]]';
With this fiddle you are going to learn the differences when you use the wildcard operator(%):
select bar from foo where bar like "%h";
select bar from foo where bar like "%h%";
select bar from foo where bar like "h%";
Check the result here: http://sqlfiddle.com/#!2/1bc45f/2
I was writing a products search engine for a pet project website and ran into a problem. How do I return products ordered by "how much of a match" they were for the query? Right now I have this, which will only return products where the query matches the beginning.
$sql = "SELECT * FROM `Products` WHERE name LIKE '$query%'";
Placing a % in front of $query like so
$sql = "SELECT * FROM `Products` WHERE name LIKE '%$query%'"
achieves the desired output but it is not ordered correctly. For example, the user searches for "p" and they get a list like: [A-word with p in it], [B-word with p in it], [P-word]. I would like the P-word to show up first, and the A & B words to show up after it. Is this possible with only MySQL? I've looked into ORDER BY but that orders by columns AFAIK.
You can do this with order by:
SELECT *
FROM `Products`
WHERE name LIKE '%$query%'"
ORDER BY (name like '$query%') desc,
name;
The first clause will put the names that start with $query first.
So I'm working on a search function for a social networking site, and it searches user posts. Some users like to put hyphens instead of spaces, but I would like for the search function look for both hyphens and spaces in a result.
For example, if they have a post named "SQL-IS AWESOME" and I search for "SQL IS AWESOME", can I still find that post? I tried using 2 sql queries, one for the original search query, and one modified to change all spaces to hyphens.
But if I search "SQL IS-AWESOME" it still won't find it. Is there an easier way?
My current code:
$sql = "SELECT * FROM posts
WHERE (post_title='".$query."'
OR post_title LIKE '%".$query."'
OR post_title LIKE '%".$query."%'
OR post_title LIKE '".$query."%')
".$locquery."
".$cat."
ORDER BY date DESC
LIMIT 18";
As someone has suggested, you could just adapt and use fulltext searching.
If you choose to take this route, you will need to enable fulltext searching on the fields required.
I'll assume you will check post_title and post_body (?), which needs you to run this;
ALTER TABLE `posts` ADD FULLTEXT KEY `post_search` (`post_title`,`post_body`);
When that is done, your search query can easily be edited to become;
$sql = "SELECT * FROM `posts` WHERE MATCH(post_title,post_body) AGAINST '$search'";
If you'd like better matching, it is also possible to give it a score and order by that, which would require code similar to this:
$sql = "SELECT *, MATCH(post_title, post_body) AGAINST ('{$search}') AS score ".
"FROM `posts` WHERE MATCH(post_title, post_body) AGAINST ('{$search}') ORDER BY `score` DESC";
--- NOTES
For the search, you need to work out how you will be searching.
In the last instance I used similar, I simply had a form for the search term (Named "Search") resulting in $_POST['search'] being sent to the server.
I then used;
$search = (array_key_exists('search', $_POST) && is_string($_POST['search'])) ? mysql_real_escape_string($_POST['search'], $c) : FALSE ;
if ($search) {
// Do the fulltext query requires (See above)
}
Since fulltext search will disregard the hyphen, you are left with just spaces, which works great for fulltext, if you opt to use scored results.