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);
Related
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.
First of all, I am not sure if my title explains this question in the correct way but I think my code will.
Instead of doing this:
SELECT * FROM mytable WHERE JSON_EXTRACT(`cars`,'$."65"') = 'Toyota' OR JSON_EXTRACT(`cars `,'$."66"') = 'Toyota' OR JSON_EXTRACT(`cars `,'$."67"') = 'Toyota'
I want to do this:
SELECT * FROM mytable WHERE JSON_EXTRACT(`cars`,any of these 65,66,67) = 'Toyota'
Can this be done?
According to the documentation, you may pass any number of paths as arguments 2 onward to the JSON_EXTRACT function. So the following should work:
SELECT *
FROM yourTable
WHERE JSON_UNQUOTE(JSON_EXTRACT(`cars`, '$."65"', '$."66"', '$."67"')) LIKE '%"Toyota"%';
Demo
Note that the WHERE clause will actually return string values in the form ["some_value"], hence I compare against this. Someone with more experience with MySQL's JSON API can probably do better than what I wrote above. But, this does at least partially answer your question; yes, you can extract multiple paths in a single call to JSON_EXTRACT.
I don't know PHP at all, so I am struggling through this. I need to add an or section to a MySQL query, but the values I'm searching have double quotes. I need to figure out how to add them in PHP so they are passed in to MySQL. The current query looks like:
$query = 'SELECT * FROM ' .$tableName.' WHERE allowed_countries LIKE "%'.$regionId.'%" and skurules REGEXP "i:'.$secondlastdigit.';" and status = 1 ORDER BY id DESC LIMIT 1';
But I need to add an or statement to search for string values that looks like:
$query = 'SELECT * FROM ' .$tableName.' WHERE allowed_countries LIKE "%'.$regionId.'%" and skurules REGEXP "i:'.$secondlastdigit.';" or skurules REGEXP "s:1:'.$secondlastdigit.';" and status = 1 ORDER BY id DESC LIMIT 1';
with double quotes surrounding the second instance of '.$secondlastdigit.'; when passed into MySQL.
My JSON string I'm searching looks like this:
a:12:{i:1;s:2:"15";i:2;s:2:"10";i:3;s:2:"30";i:4;s:2:"50";i:5;s:3:"120";i:6;s:3:"240";i:7;s:3:"480";i:8;s:3:"960";i:9;s:4:"3786";s:1:"A";s:3:"100";s:1:"C";s:2:"60";s:1:"B";s:5:"18930";}
First of all: DON'T.
If you still want to, then...REALLY DO NOT.
Making SQL queries on serialized arrays is just hell. You should try to avoid it at all costs.
Either:
Convert the serialized column into a standard SQL table
or select the column into a PHP variable, unserialize it and search through it.
Example:
$properPhpArray = unserialize($sqlResult['column_name']);
Agreed, searching serialized string is not the best solution and what the developer did despite having a bottle_size table available. I needed a quick fix and no time/skill to rewrite a tax calculation magento extension so I used replace in the query to solve my problem for now.
Since "s:1:X" will always be just one alpha character after the 1 and will not match anything else. I change the query to:
$query = 'SELECT * FROM ' .$tableName.' WHERE allowed_countries LIKE "%'.$regionId.'%" and skurules REGEXP "i:'.$secondlastdigit.';" or replace(skurules,char(34),0) REGEXP "s:1:0'.$secondlastdigit.'0;" and status = 1 ORDER BY id DESC LIMIT 1';
Very hackish fix but gets me out of a bind for now..
Mark
I have this query :
select * from users where mailaddress
NOT like '%banned_domain1.com%'
AND mailaddress NOT like '%banned_domain2.com%'
AND mailaddress NOT like '%banned_domain3.com%' ;
I want to make it more simple , I executed this query from command line :
select * from users where mailaddress
NOT like ('%banned_domain1.com%','%banned_domain2.com%','%banned_domain3.com%') ;
I got MySQL error :
ERROR 1241 (21000): Operand should contain 1 column(s)
You can use NOT REGEXP
SELECT * FROM users WHERE mailaddress NOT REGEXP 'banned_domain1.com|banned_domain2.com|banned_domain3.com';
See live demo
Instead of "Like" use "In" and format the email address like this:
select * from users where SUBSTR(mailaddress, INSTR(mailaddress, '#') + 1)
NOT IN ('banned_domain1.com','banned_domain2.com','banned_domain3.com');
The SUBSTR will remove the # and anything preceding it, leaving only the domain name then you can do a perfect comparison without wildcards using IN.
Cheers!
you have to mention the column every time
select * from tasks where title NOT LIKE '%eating lunch%' AND title NOT LIKE '%eating breakfast%' AND title NOT LIKE '%a new task%'
however as Bruno Domingues said use NOT IN that will be more easy
You cannot simplify your query. You need one LIKE per condition.
i am getting multiple values in this $languages like(php,Ruby) i want spilt that and how to check that split values in this query
SELECT * FROM software_capability where languages LIKE '%$languages%'
As in other languages Mysql also supports regexp for pattern matching that could be used in such cases. I would simply create a query string separated by the delimiter (|) out of the $languages array, and use that in the query --
$query_string = implode("|", $languages);
SELECT * FROM software_capability where languages REGEXP $query_string
The result will be same using LIKE clause as given in other answer.
Try with this.
SELECT * FROM table WHERE interests LIKE ('%sports%', '%pub%')
This is syntax. Exploded your $language and put each item in '%$item%'
SELECT * FROM software_capability where CONCAT('%',languages,'%') LIKE '$languages';
assuming that $languages is 'php,Ruby'
If $languages is comma (,) separated values, then you can try with IN command of mysql
SELECT * FROM software_capability where languages IN ($languages)