簡體   English   中英

從3個條件SQL和Codeigniter的3個表中選擇

[英]Select from 3 tables where 3 conditions SQL and Codeigniter

我在無法完成的查詢中遇到一些麻煩,主要問題是我要加入2個表並添加2個條件:

SQL Table
Products table:
id,name,section
1,Product 1,Computer
2,Product 2,Computer
3,Product 3,Computer
4,Product 4,Computer

Table1:
id,pr_id,value,qty
1,2,1

Table2:
id,pr_id,value,qty
1,1,2,1
1,2,2,2
1,3,2,3
1,3,3,1
1,3,4,1

$itemsIds = array('1', '2', '3');
$value = 2;
$q = $this->db->select('Ai.id as AiId, Ai.pr_id as AiPrId, Ai.value as AiValueId, Ai.qty as AiQty, AiR.id as AiRId, AiR.pr_id as AiRPrId, AiR.value as AiRValueId, AiR.qty as AiRQty, P.name as pName')
    ->from('products P')
    ->where_in('P.id', $itemsIds)
    ->join('table1 Ai', 'P.id=Ai.pr_id', 'left')
    ->join('table2 AiR', 'P.id=AiR.pr_id', 'left')
    ->where(array('AiValueId' => $value, 'AiRValueId' => $value))
    ->group_by('P.id')
    ->get();

主要問題在於,在此條件下,我只能獲取表1的值,而不能獲取表2的值。 我的問題是我該如何獲得帶有條件的Table1和Table2?

我相信問題在於您的where子句。 更改此:

 ->where(array('AiValueId' => $value, 'AiRValueId' => $value))

對此:

 ->where('Ai.value', $value)
 ->or_where('AiR.value',$value)

讓我知道是否可行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM