我需要按如下方式连接两个表 - 表 2“值”在表 1“price_1”、“price_2”和“price_3”上,以便我可以输出价格标签而不是价格值。不知道如何在 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中进行选择查询并获取相应的标签值并回显它们。
关于mysql join 2表选择一行字段有多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17503106/