Using the MySQL like statement - php

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

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.

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;

What are some ways to improve this long mysql search query?

Looking to improve this mysql select statement for a search query:
Select * from table WHERE ( user = '$search_query'
OR user LIKE '$search_query %'
OR keyword LIKE '$search_query'
OR tag LIKE '$search_query'
OR tag LIKE '% $search_query'
OR tag LIKE '% $search_query%'
OR tag LIKE '$search_query%'
OR REPLACE(question, ',' ,'') LIKE '$search_query %'
OR REPLACE(question, ',' ,'') LIKE '% $search_query'
OR REPLACE(question, ',' ,'') LIKE '% $search_query %'
OR REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE(question, '\'', ''), ',', ''), '.',''), ':',''), ';',''), '!',''), '?','') LIKE '%$search_query%'
)
The reason it's broke down the way it is, is because say if someone searches for "art", I don't want it showing results for "heart" as well.
I really need to have of these same functions but running fewer resources.
If your table is MyISAM, you can create a FULLTEXT index on it:
CREATE FULLTEXT INDEX fx_mytable_user_tag_question ON mytable (user, tag, question)
SELECT *
FROM mytable
WHERE MATCH(user, tag, question) AGAINST ('+some*' IN BOOLEAN MODE)
This query will return some and something but not awesome.
Actually, the first step (creating the index) is not required, however, it will speed up the queries and allow more complex searches (not limited to BOOLEAN MODE) and relevance ranging.
By default, minimal length of a search query is 4 characters, so art mentioned in your example would not be found.
To work around this, you will have to change parameter ft_min_word_len in server settings.
It looks like
OR tag LIKE '$search_query'
OR tag LIKE '$search_query%'
would return identical results to only running
OR tag LIKE '$search_query%'
Same with
OR tag LIKE '% $search_query'
OR tag LIKE '% $search_query%'
Searching around for this question, I learned that MySQL supports regular expression searches. Using these, if you are able to specify what pattern the % should match. This would help with all those pesky spaces, because you could just use \s?.
Restructure your database such that tags are stored in a separate table, with one row for each tag on a question, so that you can run a query like:
SELECT * FROM question
WHERE question_id IN (
SELECT question_id FROM question_tags WHERE tag = '$search_query'
) OR <...>
Depending on how you want search keywords to work, you may be able to use a similar solution there.

MYSQL Selecting EXACT word with MATCH query?

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}::'"

Searching a keyword in a long string in mysql?

keyword = house
SELECT * FROM products WHERE description LIKE '%house%'
This query also returns records having keyword, for example, courthouse, but I want to look for house only. How do I search anywhere in the description for "house" only?
Thanks.
UPDATE
just for more clarification ..
actually house, can be at
- the start of the description .. "House for sale..",
- can be quoted -- "house", 'house'
- can have exclamation .. house!!!, house!
- others - house? etc ..
this is why I used %house%
Are you looking for longs strings containing the whole word?
Search for "whole word match" in MySQL
SELECT * FROM products WHERE description rlike '[[:<:]]house[[:>:]]'
rlike is synonim for REGEXP.
[[:<:]] denotes the start of the word and the
[[:>:]] end of the word.
It works for all your requirements (case insensitive, with quoted words or words ending, or begging, with exclamation points and other non-letter characters)
The % before and after will search any matching text before and after the search keyword, try this instead for exact search:
SELECT * FROM products WHERE description = 'house'
Simple thing only house means, use this
SELECT * FROM products WHERE description = 'house'
If you want any word contain house in backside means use this,
SELECT * FROM products WHERE description LIKE '%house'
If you want any word contain house in frontside means use this,
SELECT * FROM products WHERE description LIKE 'house%'
If you want any word contain house in anywhere in description means use this,
SELECT * FROM products WHERE description LIKE '%house%'
Try a space around the keyword:
SELECT * FROM products WHERE description LIKE '% house %' OR description LIKE 'house %' OR description LIKE '% house' OR description LIKE '% house%'
Edit: added more options in search. The last one would allow for house. or house,, though something like housecat would also match. Regex would work best here. This solution came from Astandar, who deleted his answer.

Categories