簡體   English   中英

mysql join 2 table選擇一個多行的字段

[英]mysql join 2 table to select one row fields with multiple rows

我需要按如下方式聯接兩個表-表1'price_1','price_2'和'price_3'上的表2'value',以便我可以輸出價格標簽而不是價格值。 不確定如何在codeigniter中處理此問題。 我是否使用join然后嵌套選擇?

表格1

id  |   price_1   |   price_2  |  price_3
1   |     6       |      5     |     4

表2

 id | label | value
1   |  £6.50 | 6
2   |  £2.50 | 5
3   |  £4.00 | 4

任何指針將不勝感激。

您可以先從表1中進行選擇。然后:-

$tags = array();
foreach($record_from_table1 as $record)
{
   $tags[] = $record['price1'];
   $tags[] = $record['price2']; 
   $tags[] = $record['price3'];
}

然后制作array_unique($ tags)

從表2中進行選擇查詢,並獲取相應的標簽值並回顯它們。

感謝指針,但是基於Codeigniter多條件聯接

$this->db->select('p1.label as pa_1, p2.label as pa_2, p3.label as pa_3, p4.label as pa_4, p5.label as pa_5');
$this->db->from('table1');
$this->db->join('table2 as p1', 'p1.value = price_1', 'left');
$this->db->join('table2 as p2', 'p2.value = price_2', 'left');
$this->db->join('table2 as p3', 'p3.value = price_3', 'left');
$query = $this->db->get();

謝謝,丹

暫無
暫無

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

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