I have search items for eg '%ab', '%avcd%', %sr%', '%pple%' in my PHP variable.
I know LIKE is used in MySQL as:
Select * from tblName where fieldname like '%ab'
I have a long list of search conditions as mentioned earlier and I know that
Select * from tblName where fieldname like in ('%ab','%avcd%','%sr%','%pple%')
doesn't work. What is the best way to resolve the problem?
SELECT * from tblName where fieldname REGEXP 'ab|avcd|sr|pple';
Fulltext search is what you are really looking for.
Try this
Select * from tblName where fieldname like '%ab' or fieldname like '%ab' or fieldname like %sr%' or fieldname like '%pple%'
You can use full text indeexing if the table is myisam.
See http://dev.mysql.com/doc/refman/5.5/en/fulltext-search.html for more details.
Related
It's short and simple.
I'm using this query:
SELECT * FROM TABLE_NAME WHERE COL_NAME IN('%VALUE1%', '%VALUE2%')
However it doesn't seem to work, I've used LIKE for both values combined using OR and it returns results, so I'm certain that there's rows in the table with these values.. am I missing something here?
Does MYSQL IN() function accept percentage sign? What is the right way to do it?
Thanks in advance!
You could try with RLIKE:
SELECT * FROM TABLE_NAME WHERE COL_NAME RLIKE 'VALUE[12]'
Keep in mind these sorts of queries tend to perform very badly because they require a full table scan. If you're doing this a lot you may want to adjust your schema to better represent your usage patterns.
SELECT * FROM TABLE_NAME WHERE COL_NAME LIKE '%VALUE1%' OR COL_NAME LIKE '%VALUE2%'
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 want to create a search function on my site that will return strings within strings. For example: if there was a record in the SQL database where a column's value was "My name is Harry Potter", then the user could search for "Harry P" or "ott" or "My Name", and the record would come up in the search results.
use this in your SQL:
SELECT * FROM `table` WHERE title LIKE '%$search_word%'
select * from your_table
where instr(some_column, 'string_to_search') > 0
Use LIKE function.
SELECT * FROM myTable WHERE myName LIKE '%Harry P%'
SELECT * FROM myTable WHERE myName LIKE '%ott%'
SELECT * FROM myTable WHERE myName LIKE '%My Name%'
Here I have assume myName is the field name that you have in your database & myTable is the table name.
In PHP, your query should be
SELECT * FROM myTable WHERE myName LIKE '%$yourWord%'
If the table is / will be small, then a statement with LIKE '%word%' will be fine.
Although you might be tempted to use a MyISAM table to be able to use a Fulltext index, this is possible in InnoDB too. It will be a bit harder to implement it this way, but it will combine the high-performance text searching that MyISAM offers with all the benefits of InnoDB (transactions, row-level locking etc.).
I know WHERE, LIKE, IN filters in database. But how can I filter with "except" ? I want to select all data except a specificdata.
Thanks in advance
with NOT IN, usage is like IN
SELECT * FROM USERS WHERE ID NOT IN (1,2,3,4,5)
Three approaches:
WHERE column <> 'something'
WHERE column NOT LIKE 'something'
WHERE column not in('something')
you can use NOT IN
Note that you can use another query for NOT IN like this:
SELECT id FROM customers WHERE id NOT IN (SELECT id FROM bad_customers)
WHERE NOT LIKE
WHERE NOT IN