我在session->userdata('usershop')
中有以下数组:
Array
(
[shop_chain] => 9001
[shop_code] => 1
)
Array
(
[shop_chain] => 9002
[shop_code] => 1
)
Array
(
[shop_chain] => 9001
[shop_code] => 2
)
Array
(
[shop_chain] => 9001
[shop_code] => 3
)
我必须从另一个表中选择数据:
必须仅从此表中获取chain_code
和shop_code
匹配的行。
我试过了:
foreach ($this->session->userdata('usershop') as $usr => $usrshop) {
$where = $this->db->where(array('chain_code' => $usrshop['shop_chain'], 'shop_code' => $usrshop['shop_code']));
}
return $this->db->get('wd2dbschema_config.cfg_shop')->result();
但所有这一切都是错误的行,结果就是这样:
SELECT * FROM "wd2dbschema_config"."cfg_shop" WHERE "chain_code" = '9001' AND "shop_code" = '1' AND "chain_code" = '9002' AND "shop_code" = '1' AND "chain_code" = '9001' AND "shop_code" = '2' AND "chain_code" = '9001' AND "shop_code" = '3'
我该怎么做才能只占用匹配的行?
像这样:
SELECT * FROM "wd2dbschema_config"."cfg_shop" WHERE ("chain_code" = '9001' AND "shop_code" = '1') OR ("chain_code" = '9002' AND "shop_code" = '1') OR ("chain_code" = '9001' AND "shop_code" = '2') OR ("chain_code" = '9001' AND "shop_code" = '3')
答案 0 :(得分:1)
尝试
foreach ($this->session->userdata('usershop') as $usr => $usrshop) {
$where = $this->db->where(array('chain_code' => $usrshop['shop_chain'], 'shop_code' => $usrshop['shop_code']));
$data[] = $this->db->get('wd2dbschema_config.cfg_shop')->result();
}
return $data;
你要么执行每个where条件一次又需要在数组中捕获它们,并且在循环完成后你可以直接传递它。
答案 1 :(得分:1)
使用活动记录可以这样做,确保将where()/or_where
中的第二个参数传递为null,以便活动记录不会查找第一个参数的比较,第三个参数为FALSE,因此活动记录不会添加where子句表达式的附加bacticks
$i = 0;
foreach ($this->session->userdata('usershop') as $usr => $usrshop) {
if($i==0){
$this->db->where(" (chain_code = '" . $usrshop['shop_chain'] . "' AND shop_code = '" . $usrshop['shop_code'] . "') ", null, FALSE);
}else{
$this->db->or_where(" (chain_code = '" . $usrshop['shop_chain'] . "' AND shop_code = '" . $usrshop['shop_code'] . "') ", null, FALSE);
}
$i++;
}
return $this->db->get('wd2dbschema_config.cfg_shop')->result();