Multiple conditions in LIKE operator - php

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)

Related

SQL WHERE CONCAT LIKE NOT

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.

mySql regular expression in WHERE statement

I have a database where I store a code which is an implode("|", $array) of various codes. So the results will be something like:
100|5100|510
100|5200|510
410|5200|520
100|790|5100|320
I want to write a regular expression to search mySQL for matches from PHP ... I might have something like 100|*|510 to show me both 100|5100|510 and 100|5200|510. This RegEx works in PHP:
/100\|(?:.*)\|510/
But it does not work in mySQL. I found answers that ?: does not work, so if I remove that and use
/100\|.*\|510/
i.e. Query is:
SELECT * FROM tra_amounts WHERE coa_codes REGEXP "/100\|.*\|510/"
It shows all results from the table.
How do I write a RegEx to match some parts of the code, while leaving other parts of the code as a wildcard?
Thanks!
Have you heard of MySQL's LIKE operator? Something like this might be what you have in mind:
SELECT *
FROM yourTable
WHERE someCol LIKE '100|%|510;
Assuming that someCol had 100|5100|510 and 100|5200|510 as data, this query would return both of these records.
Use RLIKE instead of LIKE or =.

How to select rows by substring? Similar to Google Suggest [duplicate]

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);

How to search for a comma-separated string in a database table in mySQL

How to search for a comma-separated string in a database table in mySQL.
Suppose, I have a string in variable $facilities='breakfast,dinner,lunch' and in my database I have a string saved in a field called facilities having values breakfast,dinner,clothing,lunch,hot water.
How do I get the row having values $facilities?
Work like this i hope this is your requirement
select * from table where field_name like '%$fieldname%'
You're going to need to break apart the comma separated string first, and include a clause for each, so for that input you'd generate SQL like (assuming you want rows that have all three, and maybe other flags set):
SELECT *
FROM YourTable
WHERE FIND_IN_SET('breakfast', facilities)
AND FIND_IN_SET('dinner', facilities)
AND FIND_IN_SET('lunch', facilities)
You can break the string apart in PHP with something a little like:
$facilities='breakfast,dinner,lunch';
$desired = explode ( "," , $facilities );
And just use a loop to build your SQL
SELECT *
FROM tableName
WHERE FIND_IN_SET('lunch', 'breakfast,dinner,lunch')
EDIT:
SELECT *
FROM tableName
WHERE FIND_IN_SET('lunch', food_column)
food_column is the name of the column where the string 'breakfast,dinner,lunch' is stored

Getting certain parts of a string out of a select

I have a database (mydb) with following data in one column:
PK:
8/10/6101+6102 (2971386)
6110/2411 (3037457)
8/10/6504 (2276770)
8/10/6403 (2724296)
-2669938
8/1/1001-1031 (2857109)
-2547251
8/9/5003-5006 (2770096)
I have to compare these data with a variable $id for example: 2724296
SELECT * FROM mydb WHERE PK = '$id';
Of course, this query won't deliver a result. Is there a easy way to compare my $id with the values in the brace and the values with no brace? A replace in the select replaces e.g. only the brace, but not the value left to the brace.
Greets, Yab86
You can use REGEXP for this. Try this query,
$query = "SELECT * FROM mydb WHERE PK REGEXP '({$id})' OR PK REGEXP '-{$id}'";
You can use below:
SELECT * FROM mydb WHERE PK LIKE '%(2724296)';
Below is little complex: using regular expression
SELECT * FROM mydb WHERE PK REGEXP '[(]2724296[)]';
See this https://dev.mysql.com/doc/refman/5.1/en/regexp.html for writing customizable regular expression.
I would try a regular expression (assuming you only want to check the number inside the brackets). Something like this should work:
\([0-9]*\)$
This will return a string that begins with ( followed by 1 or more numbers and ends with ). You should then be able to remove the brackets from the string and compare it with your variable

Categories