ถ้าต้องการ where array ด้วย key-value ต้องทำอย่างไร [mySql]
Code (PHP)
$data = array('0100' => 'Dog','0200' => 'Cat','0300' => 'Bird','0400' => 'Fish');
$dataKey = array_keys($data);
$str = implode(',', $dataKey);
$sql = sprintf("SELECT * FROM Pet where FIND_IN_SET(animal_id,'%s')",$str);
ประวัติการแก้ไข 2017-02-02 10:00:03
Date :
2017-02-02 09:58:36
By :
Luz
Code (PHP)
$string="0100,0200,0300,0400";
$array=array_map('intval', explode(',', $string));
$array = implode("','",$array);
$sql = "SELECT * FROM Pet WHERE animal_id IN ('".$array."')" ;
echo $sql;
Result
SELECT * FROM Pet WHERE animal_id IN ('100','200','300','400')
Date :
2017-02-02 10:17:23
By :
mr.win
ถ้ามี Key,Value
Code (PHP)
<?php
$array = array(
"0100" => "Dog",
"0200" => "Cat",
"0300" => "Bird",
"0400" => "Fish",
);
$dataKey = array_keys($array);
$str = implode("','",$dataKey);
$sql = "SELECT * FROM Pet WHERE animal_id IN ('".$str."')" ;
echo $sql;
?>
Result
SELECT * FROM Pet WHERE animal_id IN ('0100','0200','0300','0400')
Date :
2017-02-02 10:28:29
By :
mr.win
รบกวนเพิ่มครับ แล้วถ้าใน Java ตรงตัวแปร ควรเป็นอย่างไรดีครับ
Date :
2017-02-02 10:32:27
By :
yokai
Load balance : Server 04