I have this SQL query:
SELECT * FROM pages WHERE page_title = 'paul_mccartney'
But often in page_title the value is like 'paul_mccartney (musician)'. And in this case I get NULL back. I had the idea to regex replace everything which is between ( and ) including the ():
SELECT * FROM pages WHERE REPLACE(REGEXP('/\([^\*].*\)/U'), '', page_title) = 'paul_mccartney'
But it doesn't work. Is my idea possible or not? And how?
Although your question is about using RegEx but have you looked into MySQL fulltext search functions?
https://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html
https://dev.mysql.com/doc/refman/5.1/en/fulltext-boolean.html
Using the following tutorials if you have a row that is like this:
Id title
1 paul_mccartney (musician)
Using the example SQL provided will return a result:
SELECT * FROM articles
WHERE MATCH (title) AGAINST ('paul_mccartney' IN BOOLEAN MODE)
You could try a LIKE and see how that works:
SELECT * FROM pages WHERE page_title LIKE '%paul_mccartney%'
Related
I'm looking at an application where we can tag items. Currently, if we search for more than 1 tag, then results with content for with either tag appears. it is inclusive rather than exclusive. This is the code which causes it:
//app/content/Search/Mysql/Query.php:232
$tagWhere = array( $itemsOnlyTagSearch . \GDP\Db::i()->in( 'index_item_index_id', $tagIds ) );
For example, if I search for the tags "windows" and "ios" then content with windows OR iOS tagged come back.
I want to update that line of code so only content with BOTH "windows" and "ios" tagged come back. How can I do that?
What library are you using to construct your queries?
Right now, your query is something like
SELECT * FROM TABLE_NAME WHERE index_item_index_id in (id1, id2)
where id1 and id2 refers to the tagIds.
You need to modify your query to use multiple where column = value expressions, e.g.
SELECT * FROM TABLE_NAME WHERE index_item_index_id = id1 AND index_item_index_id = id2)
It depends on your ORM / Query Class how to construct such SQL query in PHP.
I try to make SQL to search some string in database.
In this spesification, The SQL must be dont display one string in database.
my sql like this :
$query = "SELECT * FROM `chatuser` WHERE CONCAT( `fullname`,`image`) LIKE '%".$search_string."%' NOT (`$string is not be displayed`) " ;
is that possible ?
Thanks for help
The correct syntax of LIKE and NOT LIKE as two conditions would be:
SELECT * FROM chatuser
WHERE CONCAT(CustomerName,ContactName) LIKE '%t%'
AND CONCAT(CustomerName,ContactName) NOT LIKE '%m%';
You miss AND Between conditions. Also you have to repeat CONCAT(CustomerName,ContactName).
In the example above we are looking for all CustomerName+ContactName with a t in any place but if it doesn't have an m in any place.
From the docs found at https://www.w3resource.com/mysql/comparision-functions-and-operators/not-like.php
Example: MySQL NOT LIKE operator with (%) percent
The following MySQL statement excludes those rows from the table author, having the 1st character of aut_name ‘W’.
Code:
SELECT aut_name, country
FROM author
WHERE aut_name NOT LIKE 'W%';
And so it seems would work in your situation.
I have mysql table that has a column that stores xml as a string. I need to find all tuples where the xml column contains a given string of 6 characters. Nothing else matters--all I need to know is if this 6 character string is there or not.
So it probably doesn't matter that the text is formatted as xml.
Question: how can I search within mysql?
ie
SELECT * FROM items WHERE items.xml [contains the text '123456']
Is there a way I can use the LIKE operator to do this?
You could probably use the LIKE clause to do some simple string matching:
SELECT * FROM items WHERE items.xml LIKE '%123456%'
If you need more advanced functionality, take a look at MySQL's fulltext-search functions here: http://dev.mysql.com/doc/refman/5.1/en/fulltext-search.html
Using like might take longer time so use full_text_search:
SELECT * FROM items WHERE MATCH(items.xml) AGAINST ('your_search_word')
SELECT * FROM items WHERE `items.xml` LIKE '%123456%'
The % operator in LIKE means "anything can be here".
Why not use LIKE?
SELECT * FROM items WHERE items.xml LIKE '%123456%'
you mean:
SELECT * FROM items WHERE items.xml LIKE '%123456%'
When you are using the wordpress prepare line, the above solutions do not work. This is the solution I used:
$Table_Name = $wpdb->prefix.'tablename';
$SearchField = '%'. $YourVariable . '%';
$sql_query = $wpdb->prepare("SELECT * FROM $Table_Name WHERE ColumnName LIKE %s", $SearchField) ;
$rows = $wpdb->get_results($sql_query, ARRAY_A);
Ok so I have a very big table and I need to search a url which have a keyword in it, and I am trying to do it with LIKE but LIKE is working just like this 'foo%' and this checks if the string starts with foo and what I try to get is '%foo%'.
$query = mysqli_query($link,"SELECT * FROM table WHERE url LIKE 'foo%' LIMIT number");
If you can give me an idea it would be perfect. Thanks!
Also I have an index for url and id.
Using LIKE will slow the query down; one idea would be to try LOCATE instead:
$query = mysqli_query($link,"SELECT * FROM table WHERE LOCATE('foo', url) > 0 LIMIT number");
LOCATE will simply find the index of the match else return 0, similar to strpos() in PHP (except strpos() will return false if no match). There is no wildcard matching.
So here is the scenario:
MySQL
have 1 MYISAM Table
colum named v.value has a full text index
Basic query works fine, uses the index as expected:
SELECT p.online_identifier
FROM (...)
WHERE r.area_id = 3 AND s.state_id= 4
AND (snap.area_has_catalogues_attributes_id = 7028
AND MATCH (v.value) AGAINST('+SomeBrand' IN BOOLEAN MODE))
Now when I add an OR, the full text search index (on v.value) is not used.
I run Explain to verify it.
The query would look something like this:
(...)
WHERE r.area_id = 3 AND s.state_id= 4 AND
(snap.area_has_catalogues_attributes_id = 7028 AND MATCH (v.value) AGAINST('+SomeBrand' IN BOOLEAN MODE))
OR (snap.area_has_catalogues_attributes_id = 7045 AND MATCH (v.value) AGAINST('+OtherBrand' IN BOOLEAN MODE))
I dont understand why.
Any ideas?
Here is something interesting: The query you have is actually causing the FULLTEXT indexing to be ignored. Don't worry, it is not your fault. I wrote about this before : Is there a way to hint to query optimizer to MySQL which constraints should be done first?
After looking over my answer and its subsequent links, now let's look at your original query:
SELECT p.online_identifier
FROM (...)
WHERE r.area_id = 3 AND s.state_id= 4
AND (snap.area_has_catalogues_attributes_id = 7028
AND MATCH (v.value) AGAINST('+SomeBrand' IN BOOLEAN MODE))
You will have to execute the FULLTEXT search alone and retrieve IDs only.
Use those IDs to join to the other table aliases.
Let me take a query from one of my other links to demonstrate what to do to your query
Instead of this query
select * from seeds WHERE MATCH(text) AGAINST
("mount cameroon" IN BOOLEAN MODE) = 4;
you must refactor it into something like this:
SELECT B.* FROM
(
SELECT id,MATCH(text) AGAINST
("mount cameroon" IN BOOLEAN MODE) score
FROM seeds
WHERE MATCH(text) AGAINST
("mount cameroon" IN BOOLEAN MODE)
) A
INNER JOIN seeds B USING (id)
WHERE A.score = 4;
Give it a Try !!!