table_car.id as carId, $this->table_car.price as -6ren">
gpt4 book ai didi

mysql - 在选择查询表名称上使用变量

转载 作者:行者123 更新时间:2023-11-29 11:44:18 25 4
gpt4 key购买 nike

我需要在模型选择查询上使用动态表名称,如下所示:

$this->db->select("$this->table_car.id as carId, $this->table_car.price as carPrice, $this->table_car.name as carName, $this->table_car.used as carUsed, $this->table_car.visible as carVisible, $this->table_cat.name as catName, $this->table_brand.name as carBrand, $this->table_model.name as carModel");

但是变量不起作用。这是我得到的:

SELECT .`id` AS `carId`, .`price` AS `carPrice`, .`name` AS `carName`, .`used` AS `carUsed`, .`visible` AS `carVisible`, .`name` AS `catName`, .`name` AS `carBrand`, .`name` AS `carModel`
LEFT JOIN ON .`car_id` = .`id`
LEFT JOIN ON .`id` = .`cat_id`
LEFT JOIN ON .`id` = .`brand_model_id`
LEFT JOIN ON .`id` = .`model_id`
LEFT JOIN ON .`id` = .`brand_id`
WHERE .`used`= 0 LIMIT 3

有解决办法吗?

提前致谢

最佳答案

虽然双引号确实允许在其中使用变量,而无需关闭和连接,但它并不总是适用于喜欢或对象和数组,就像您所展示的那样。更好的解决方案是:

$this->db->select($this->table_car . ".id as carId, " 
. $this->table_car . ".price as carPrice, "
. $this->table_car . ".name as carName, "
. $this->table_car . ".used as carUsed, "
. $this->table_car . ".visible as carVisible, "
. $this->table_cat . ".name as catName, "
. $this->table_brand . ".name as carBrand, "
. $this->table_model . ".name as carModel");

另一种选择是将对象变量放在大括号 {} 中,例如:

$this->db->select("{$this->table_car}.id as carId, 
{$this->table_car}.price as carPrice,
{$this->table_car}.name as carName,
{$this->table_car}.used as carUsed,
{$this->table_car}.visible as carVisible,
{$this->table_cat}.name as catName,
{$this->table_brand}.name as carBrand,
{$this->table_model}.name as carModel");

关于mysql - 在选择查询表名称上使用变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35223612/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com