数据库:
选
| ID | TITLE | COMPANY_ID |
| 1 | One | 1 |
| 2 | One | 1 |
| 3 | One | 1 |
公司
| ID | NAME |
| 1 | One |
| 2 | Two |
| 3 | Three |
我有PDO MYSQL声明:
"SELECT
listings.ID,
listings.TITLE,
listings.COMPANY_ID,
companies.ID,
companies.NAME
FROM listings INNER JOIN companies ON (listings.COMPANY_ID = companies.ID ) WHERE (listings.TITLE = :p1 AND companies.NAME = :p2 )LIMIT 10"
关联数组输出:
Array
(
[ID] => 1
[TITLE] => analyst
[COMPANY_ID] => 1
[NAME] => one
)
Array
(
[ID] => 1
[TITLE] => analyst
[COMPANY_ID] => 1
[NAME] => one
)
Array
(
[ID] => 1
[TITLE] => analyst
[COMPANY_ID] => 1
[NAME] => one
)
执行此查询时,我输出到PHP fetch关联数组的ID字段将返回为1。
除ID外,输出按预期工作。
为什么我的ID值会发生变化?
______________________
方法:
protected function search_joined($parameters, $func){
$field = "*";
if(isset($parameters["fields"])){
if($parameters["fields"] != "*" && gettype($parameters["fields"]) == gettype(array())){
if(count($parameters["fields"]) == 2){
$field = "";
foreach($parameters["fields"] as $key=>$v){
foreach($v as $v_){
$field.= $parameters["tables"][$key] . "." . $v_ . ",";
}
}
$field = rtrim($field, ",");
}
}
}
$cond_ = "";
$values = array();
if(gettype($parameters["condArry"]) == gettype(array())){
$COND_TYPE = " AND ";
foreach($parameters["condArry"] as $v){
$operator = " = ";
if($v[1][0] == "%" || substr($v[1], -1) == "%"){
$operator=" LIKE ";
}
if(substr($v[1], 5) == "L::_>"){
$operator=" > ";
$v[1] = str_replace($v[1], "L::_>", "");
}
if($v[1][0] == "!"){
$operator=" != ";
//$v[1] = str_replace($v[1], "!", "");
$v[1] = substr($v[1], 1);
}
$COND_TYPE = (
(isset($v[2]))?
(
($v[2] == "&")? " AND " :
(
(($v[2]=="||")? " OR ": "")
)
): " AND "
);
$unique = md5($v[0] . $v[1]);
$cond_.= $v[0] . $operator . ":".substr($v[0], strpos($v[0], ".") + 1).$unique. " " . $COND_TYPE . " ";
$values[':'.substr($v[0], strpos($v[0], ".") + 1).$unique] = $v[1];
}
$cond_ = "WHERE (" . substr($cond_, 0, strlen($COND_TYPE)*(-1)) . ")";
}
//$cond_ = rtrim($cond_, ",");
$joiner = $parameters["tables"][0] . "." . $parameters["joiner"][0] . "=" . $parameters["tables"][1] . "." . $parameters["joiner"][1] . " ";
$sql = "SELECT ". $field . " FROM " . $parameters["tables"][0] . " INNER JOIN " . $parameters["tables"][1] . " ON (" . $joiner . ") " .$cond_ . (isset($parameters["LIMIT"])? "LIMIT " . $parameters["LIMIT"]: "");
echo $sql;
if(isset($parameters["test"])){
if($parameters["test"] == true){
echo "<br>". $sql . "<br>";
}
}
//echo "<br>". $sql . "<br>";
$q = $parameters["connection"]->prepare($sql);
foreach($values as $key => $v){
$q->bindvalue($key, $v);
echo "<br>" . $key . " : " . $v . "<br>";
}
$q->execute();
while($row = $q->fetch(PDO::FETCH_ASSOC)) {
if(!$func($row)){
break;
}
}
$conTableArry = null;
}
CALLER:
$params_= array(
"connection"=>$this->connection_,
"tables"=>array("listings", "companies"),
"joiner"=>array("COMPANY_ID","ID"),
"fields"=> array(
array(
"ID",
"TITLE",
"PAYMIN",
"PAYMAX",
"GEO_LOCATIONS_ID",
"CRIMINAL_HISTORY_REQ",
"EVAL_EXAM_REQ",
"AGE_REQ",
"EX_REQ",
"DRIVERS_LICENSE_REQ",
"CERTIFICATE_REQ",
"EDUCATION_REQ",
"RESUME_REQ" ,
"POSITIONS",
"COMPANY_ID"
),
array(
"ID",
"NAME"
)
),
"condArry"=>array(array("listings.TITLE", "analyst"), array("companies.NAME", "suitespec")),
"LIMIT"=>$LIMIT,
);
parent::search_joined($params_, function($r){
out($r);
return true;
});
答案 0 :(得分:3)
你的SQL中有一些令人困惑的语法
SELECT
listings.ID,
listings.TITLE,
listings.COMPANY_ID,
companies.ID,
companies.NAME
FROM listings
INNER JOIN companies ON (listings.COMPANY_ID = companies.ID )
WHERE (listings.TITLE = :p1 AND companies.NAME = :p2 )
LIMIT 10
您有两个ID
列,没有别名。所以你可能会得到第二个ID
。尝试对第二个ID
列进行别名并再次尝试
SELECT
listings.ID,
listings.TITLE,
listings.COMPANY_ID,
companies.ID AS comp_id,
companies.NAME
答案 1 :(得分:1)
您应该能够通过为结果集使用唯一的列名来解决此问题(您目前使用ID
两次)。
将SQL更改为以下内容:
"SELECT
listings.ID,
listings.TITLE,
listings.COMPANY_ID,
companies.ID AS CID,
companies.NAME
FROM listings
INNER JOIN companies ON (listings.COMPANY_ID = companies.ID )
WHERE (listings.TITLE = :p1
AND companies.NAME = :p2 )LIMIT 10"
注意第5行。我已将其从companies.ID
更改为companies.ID AS CID
。