语言:PHP
DB:MySQL的
我的理解是,如果你的IN()子句中有重复的条目..他们将跳过......我需要一个返回/排为IN()子句中的每个项目。如果不考虑他们的DUP与否。
从阅读...我相信[自己] JOIN()是方法/解决方案,我正在寻找..但我不是如何做到这一点JOIN在同一个表清楚了吗? ..尤其是我已经低于去查询。 (工作我怎么需要它..期待的IN()子句中返回的重复条目的行)
但是..我也了解EXISTS()以及..也许是一个更好的办法? (我不是在寻找的速度在这里..我正在寻找一个可行的解决方案)
我有显示同一个表rextexter例子。(但这个查询请求也对rextester工作,但不能诠释,他的现实世界还是WAMP..etc)
http://rextester.com/SZRIAN96972
所以我不太清楚,如果它的实际查询本身? (我读会是问题,因为的DUP将在IN()子句中跳过)...因为它“作品”在WAMP和REXTESTER ...
因此,也许它的PDO操作的一部分? 也许在数组中传递给执行()函数,当有事情发生?
PDO段:
$qMarks = str_repeat('?,', count($brandlist) - 1) . '?'; //create '?' mark placeholders for query, remove last comma and replace with '?'
//preserve IN() order
$displayList_sql = "SELECT * FROM $tablename WHERE CONCAT(brandname, ' ', dosage) IN ($qMarks) ORDER BY FIELD(CONCAT(brandname, ' ', dosage),'". trim(implode("','", $brandlist))."')";
$displayList_stmt = $conn->prepare($displayList_sql);
$displayList_stmt->execute($brandlist);//make note of passing in array as param to execute() call
的$ brandList阵列的实施例被传递在:(带的DUP)
Zyflo CR Extended-release tablet 600 mg','Zyflo CR Extended-release tablet 600 mg',' SEE NOTES BELOW'
所以在这一点..我不清楚,如果IN()子句是问题? 或者,如果同时通过在阵列中的execute()命令的东西正在做? 有什么东西剥离/在IN()子句中跳过式两份(S)? 我怎样才能避免这种情况? 我希望它是某种自我的加盟..但所有的例子,我看到有硬编码值(不能)..然后位于初始IN()子句中的重复的条目在第二查询或东西被使用。 (我是相当以下为我不可能看不出这适用于我的情况)
这里是一个工作示例的快速倾倒,我已经在我的本地WAMP运行安装:(需要你自己的连接包括文件)
require_once('../db_wamp_pdo.php');
$tablename = 'xxx';
//hard coded list for testing
$brandList_og = array('Zyflo CR Extended-release tablet 600 mg','Zyflo CR Extended-release tablet 600 mg',' SEE NOTES BELOW', 'Alvesco HFA 80mcg');
function get_displayList($tablename, $conn, $brandlist){
$rowcount = 0;
if($brandlist != '' && count($brandlist) > 0){
$qMarks = str_repeat('?,', count($brandlist) - 1) . '?'; //create '?' mark placeholders for query, remove last comma and replace with '?'
//$displayList_sql = "SELECT * FROM $tablename WHERE CONCAT(brandname, ' ', dosage) IN('Zyflo CR Extended-release tablet 600 mg','Zyflo CR Extended-release tablet 600 mg',' SEE NOTES BELOW', 'Alvesco HFA 80mcg') ORDER BY FIELD(CONCAT(brandname, ' ', dosage),'Zyflo CR Extended-release tablet 600 mg','Zyflo CR Extended-release tablet 600 mg',' SEE NOTES BELOW', 'Alvesco HFA 80mcg')";
$displayList_sql = "SELECT * FROM $tablename WHERE CONCAT(brandname, ' ', dosage) IN($qMarks) ORDER BY FIELD(CONCAT(brandname, ' ', dosage),'". trim(implode("','", $brandlist))."')";
//$displayList_sql = "SELECT * FROM $tablename WHERE CONCAT(brandname, ' ', dosage) FIND_IN_SET($qMarks) ORDER BY FIELD(CONCAT(brandname, ' ', dosage),'". trim(implode("','", $brandlist))."')";
//$displayList_sql = "SELECT * FROM $tablename WHERE EXISTS(SELECT CONCAT(brandname, ' ', dosage)) ORDER BY FIELD(CONCAT(brandname, ' ', dosage),'". trim(implode("','", $brandlist))."')";
$displayList_stmt = $conn->prepare($displayList_sql);
$displayList_stmt->execute($brandlist);//make note of passing in array as param to execute() call
$displayList_stmt->setFetchMode(PDO::FETCH_ASSOC);
$_displayList = $displayList_stmt->fetchAll(); //returns multi-dimensional array (and correct count)
$colcount = $displayList_stmt->columnCount();
$rowcount = $displayList_stmt->rowCount();
}
if($rowcount <= 0){
//nothing returned
}else{
return $_displayList;
}
}
$my_displayList = get_displayList($tablename, $conn, array_values(array_filter($brandList_og)));//array_filter() added to get count of only non empty indexes
echo 'BRAND LIST: <br>';
echo var_dump($my_displayList);
echo '<br>';
我一直在输出沿途调试文本..和“阵”是罚款,直到它来自查询回来..
因此,它无论是IN()子句(我目前的理论),我需要帮助以某种方式加入自连接到这个动态查询? 或(较少可信)阵列中传递给执行()线时..事情变得解析出/删除?