简体   繁体   English

mysql join 2 table选择一个多行的字段

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

I need to join two table as follows - table 2 'value' on table 1 'price_1', 'price_2' & 'price_3' so I can output price label instead of the price value. 我需要按如下方式联接两个表-表1'price_1','price_2'和'price_3'上的表2'value',以便我可以输出价格标签而不是价格值。 Not sure how approach this in codeigniter. 不确定如何在codeigniter中处理此问题。 Do I use join the then nested select?: 我是否使用join然后嵌套选择?

table 1 表格1

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

Table 2 表2

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

Any pointers would be appreciated. 任何指针将不胜感激。

You can first make a select from table 1. Then:- 您可以先从表1中进行选择。然后:-

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

Then make array_unique($tags) 然后制作array_unique($ tags)

Make select query from table 2 and get corresponding label values and echo them. 从表2中进行选择查询,并获取相应的标签值并回显它们。

thanks for pointers, but did it this way, based on Codeigniter Join with Multiple Conditions 感谢指针,但是基于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();

Thanks, Dan 谢谢,丹

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM