PHP CodeIgniter WHERE子句声明

时间:2018-01-06 00:25:42

标签: php mysql codeigniter

我有一个非常基本的陈述,我正在尝试运行,我遇到了问题。

$item = $this->db
            ->select("r.CustomerIDs, r.DateAdded")
            ->join("customer_orders_rewards as cor", "r.RewardID = cor.RewardID")
            ->join("customer_orders as co", "co.OrderID = cor.OrderID")
            ->where(array("r.Denomination" => $row['Denomination'], "r.RewardID" => "cor.RewardID"))
            ->get("customer_rewards as r");

在上面的陈述中,它将cor.RewardID解释为字符串,我希望它是来自join的字段。

这导致查询看起来像这样:

SELECT `r`.`CustomerID`, `r`.`DateAdded` 
FROM `customer_rewards` as `r` 

JOIN `customer_orders_rewards` as `cor` 
ON `r`.`RewardID` = `cor`.`RewardID` 

JOIN `customer_orders` as `co` 
ON `co`.`OrderID` = `cor`.`OrderID` 

WHERE `r`.`Denomination` = '35' 
AND `r`.`RewardID` = 'cor.RewardID'  <---- Issue

如何在我的join子句中引用WHERE中的字段?

1 个答案:

答案 0 :(得分:2)

一个简单的解决方法是使用

"r.RewardID = cor.RewardID"

而不是

"r.RewardID" => "cor.RewardID"

这样,cor.RewardID不应被视为字符串文字,而应视为实际列。

在旁注中,您r.RewardID = cor.RewardID时已经加入了行,所以我说额外条件是多余的(有问题的),不需要它。