How to compare Comma Seperated value to get existing value - mysql

I have Table as Production with field name as production_code
Production_code have value like this,
Id production_code
1 P101,P102,P103,P105
2 P103,P106,P102
3 P104
4 P102,P105,P111
------ I have value on PHP page like $p_code='P102,P109';
Now I want to fetch rows from Table Production_code where any code is exist in production_code of variable $p_code
please help me .. what mysql query should i use

You can use function FIND_IN_SET, like:
SELECT * FROM yourTable
WHERE FIND_IN_SET('P102', production_code) OR FIND_IN_SET('P109', production_code)

Solution.
<?php
$p_code='P102,P109'; //example
$condition = '';
foreach(explode(',',$p_code) as $r){//explode convert $p_code string into array. then apply foreach for compare every element of array From database saved value
$condition .= 'FIND_IN_SET("'.$r.'", production_code) OR ';
}
$condition = rtrim($condition,' OR '); //this remove last occurence of OR from condition
echo $sql = 'SELECT * FROM yourTable
WHERE '.$condition;
?>

Related

Mysql - select all columns of a single row with its values and column comments

Here is the query to get all columns of a single row
$STH = $DBH->db->prepare("SELECT * FROM Settings");
$STH->execute();
$STH->setFetchMode(PDO::FETCH_ASSOC);
$str = '';
while ($row = $STH->fetch())
{
foreach($row as $key => $value)
{
$str .= '<div>'.$key.' => '.$value.'</div>';
}
}
and this one is to get extended information of the column (including comment)
$STH = $DBH->db->prepare("SHOW FULL COLUMNS FROM Settings");
$STH->execute();
$STH->setFetchMode(PDO::FETCH_ASSOC);
\* the same php code *\
can I combine these two SQL queries to get the following data simultaniously
`FieldName`, `FieldValue`, `CommentValueOfColumn`
'
'
'
example of Settings table
Columns -> | PrintImage | SettMinus | HasChat . . .
--------------------------------------------
Values -> 0 1 1 . . .
and there is additional data Comment of the column.
So I want to select data:
PrintImage, 0 , Comment of PrintImage;
SettMinus, 1 , Comment of SettMinus;
HasChat, 1 , Comment of HasChat;
.....................................
and so on.
This will give you the column name, values of the columns and the comments. However, you can use php to get the values which is
VALUEARRAY[ORDINAL_POSITION]
You also need to copy/paste the rest of the column names after concat_ws.
Again, I cannot do everything in mysql so it needs help from PHPcodes to get the exact value from the array.
SELECT s.COLUMN_NAME,
s.ORDINAL_POSITION,
t.valuearray,
s.COLUMN_COMMENT
from INFORMATION_SCHEMA.columns s
cross join (select concat_ws(",", PrintImage,
SettMinus,HasChat) as valuearray from settings) t
where s.TABLE_NAME='Settings';
sample result :
COLUMN_NAME ORDINAL_POSITION valuearray COLUMN_COMMENT
PrintImage 1 0,1,1 commenttest
SettMinus 2 0,1,1 comments too
HasChat 3 0,1,1 3rd comment

Skipping a query field on IF condition

My MySQL query is sent to a csv, and then to a HTML file.
When I do...
SELECT
field_a,
IF($VAR="yes",field_b,""),
field_c
FROM table
INTO OUTFILE "query.csv";
...I always get 3 columns (field_a, field_b or empty, field_c), as expected.
How can I get only 2 columns (field_a, field_c) when $VAR is not "yes"?
Build your query dynamically. It looks like your $VAR condition does not depend on the data. Then you may do this (example in PHP):
$fields = 'field_a' ;
if ($VAR == 'yes') $fields .= ', field_b' ; // and so on
$query = "SELECT $fields FROM table" ;

Select record using FIND_IN_SET

I am storing the check box value as comma separated value in db like
Features
1,3,4
1,2,3,4,5
3,4,5,6
Now while searching i need to select the record based on the user select the check-box, say example if user selects 1 and 6 needs to fetch matched record. I am getting selected check-box value as array like (0=>1,1=>6). I dont know how to loop through this like FIND_IN_SET(1,Features) OR FIND_IN_SET(6,Features)
It has to come like
SELECT * FROM table WHERE FIND_IN_SET(1,Features) OR FIND_IN_SET(6,Features)
$str='%';
foreach($array as $val)
{$str.=$val.'%';}
SELECT * FROM table WHERE Features like $str
Update:
$str='';
foreach($array as $val)
{$str.='or Feature like %'.$val.'%';}
$str[0]='';$str[1]='';
SELECT * FROM table WHERE $str
I fixed using the below code to get query like this SELECT * FROM table WHERE FIND_IN_SET(1,Features) OR FIND_IN_SET(6,Features)
if($selCount >1){
foreach($luxPropFeatures as $val) {
$str .="OR FIND_IN_SET('".$val."',LP.luxury_property_feature_id) ";
$trimmed = ltrim($str, "OR");
//$str[0]='';$str[2]='';
}
}else{
foreach($luxPropFeatures as $val) {
$trimmed = "FIND_IN_SET('".$val."',LP.luxury_property_feature_id) ";
}
}

MYSQL & C#: How to escape the elements in a SELECT WHERE IN (...) clause?

I have procedure with a single string parameter to retrieve records from my table test which has two fields id(int) and Name(varchar).
the query in the procedure is shown below
Select * from test where id in (strParam);
and value in the parameter will be
strParam="1,2";
but the result will be wrong because query will be as shown below
Select * from test where id in ('1,2');
but i need the query to be like shown below
Select * from test where id in (1,2);
please help me with a solution
the programming language is C#
thanks,
suraj
Usually you construct the SQL correctly in your programming language:
Select * from test where id in ('1,2');
should come from your application code, where it's easier to change strParam="1,2"; to strParam="'1','2'":
Split (explode) the string into an array
escape each element in the array (using the correct MySQL-ESCAPE function)
Join (implode) the array back into a string,
If you really can't change the application code, maybe some SQL tricks could work. Try:
SELECT * FROM test where FIND_IN_SET(ID,strParam) > 0
Not sure if this is the most efficient way:
Explode the value strParam to an array and then build up the string you need in the query:
<?php
$arrayParam = explode(',', $strParam);
$strParamQuery = '(';
foreach ($arrayParam as $Param) {
if ($strParamQuery != '(') { $strParamQuery = $strParamQuery.','; //Add a comma to all but the first occurence
$strParamQuery = $strParamQuery.$param;
}
$strParamQuery = $strParamQuery.')';
$query = 'Select * from test where id in '.$strParamQuery.';';
?>

mySQL query -- if value in string

I have a string of IDs separated with comma
$myIDs = 22,23,45,895;
How do I write a query to return records for values that correspond to the IDs in my string?
This does not seem to be right:
SELECT *
FROM t1
WHERE itemID IN ($myIDs)
I guess I'm trying PHP array function here, hah? Is there something like this in mySQL?
Appreciate any suggestions. Thanks.
I think you're missing quotes, ie, the exact query should look like this before evaluation
SELECT *
FROM t1
WHERE itemID IN ('22','23','45','895');
Hence all you've got to do to fix this is:-
$myIDs = array(22,23,45,895);
$myIDs_string = "'".implode("','",$myIDs)."'";
then in whatever PHP/SQL library/framework you select, use PHP to execute the following php query:-
SELECT *
FROM t1
WHERE itemID IN ($myIDs_string);
Hope this helps.
$IDs = array(1,2,3,4,5);
// alternatively, you can write it like this...
// $IDs = "1,2,3,4,5";
if(is_array($IDs))
$IDs = implode(",",$IDs);
$query = "SELECT * FROM t1 WHERE itemID IN ($IDs)";
echo $query;