从mysql字段中检索的索引中提取php数组值

时间:2014-02-06 10:10:12

标签: php mysql arrays associative

有没有办法从mysql字段中检索索引中提取php数组值?

我的意思是,我有这两张桌子:

vechicle_types表:

vt_id | vt_type
1     | car
2     | van
3     | truck
4     | bike

vechicles表:

vehicle_brand | vechicle_model | vehicle_type
brand 1       | model 1        | 1
brand 1       | model 2        | 3
brand 1       | model 3        | 2
brand 2       | model 1        | 4
brand 2       | model 2        | 1
brand 2       | model 3        | 4

然后,我通过一些函数在一些计算中动态生成了一个tax数组($arr_taxes),所以它不能存储在数据库表中,因为这些值是内联计算的

现在我想根据存储在数据库中的车辆类型(vehicle_tax)创建一个从$arr_taxes中提取的值的SQL变量(vehicle_type

这是我的代码,当然它不起作用:

$arr_taxes=(1=>10, 2=>15, 3=>20, 4=>12); # this array is dinamically generated previously from some calculations

$var_query="SELECT vehicle_brand, vechicle_model, vehicle_type, {$arr_taxes[vechicle_type]} as vehicle_tax FROM vehicles";

有没有办法完成我的需要?

提前谢谢

更新信息:

实际上,表格更像是

vehicle_brand | vechicle_model | vehicle_type  | vehicle_price
brand 1       | model 1        | 1             | 300
brand 1       | model 2        | 3             | 150
brand 1       | model 3        | 2             | 200
brand 2       | model 1        | 4             | 100
brand 2       | model 2        | 1             | 300
brand 2       | model 3        | 4             | 600

然后,查询应如下所示:

$var_query="SELECT vehicle_brand, vechicle_model, vehicle_type, vehicle_price, {$arr_taxes[vechicle_type]} as vehicle_tax, (vehicle_price+(vehicle_price*({$arr_taxes[vechicle_type]}/100)) as vehicle_finalprice FROM vehicles ORDER BY vehicle_finalprice"; 

0 个答案:

没有答案