【发布时间】:2021-07-29 09:23:50
【问题描述】:
我正在使用 Laravel 8.12
我正在使用 DB::raw() 方法过滤进行 DB::select() 调用。但为了方便起见,我也会发布带有值的完整声明。
这里是查询的PHP代码
$sql = "SELECT `medium_info`.* , `postings`.`posting_timestamp` FROM `postings` INNER JOIN `medium_info` ON `postings`.`medium_info_id` = `medium_info`.`id` INNER JOIN `accounts` ON `accounts`.`id` = `postings`.`account_id` INNER JOIN `merchants` ON `merchants`.`account_holder_id` = `accounts`.`account_holder_id` INNER JOIN `medium_types` ON `medium_types`.`id` = `accounts`.`medium_type_id` WHERE `merchants`.`account_holder_id` = :merchant_account_holder_id AND `medium_info`.`id` = :medium_info_id AND `medium_types`.`id` = :medium_types_id";
$result = DB::select ( DB::raw($sql), ['merchant_account_holder_id'=>230124, 'medium_info_id'=>551678, 'medium_types_id'=>1] );
当我打印 $result 时,它会给我这样的数据:
[0] => stdClass Object
(
[id] => 230124
[purchase_date] => 2020-11-22
[redemption_date] =>
[expiration_date] =>
[hold_until] => 2021-05-07 02:30:08
...more medium_info data here
[posting_timestamp] => 2020-11-25 23:27:13
...merchants table data which I did not request
[account_holder_id] => 230124
[name] => Best Buy
[logo] => /cdn/merchants/230124/logo.png
如果我执行以下操作,结果仍然相同:
$sql = "SELECT `medium_info`.* , `postings`.`posting_timestamp` FROM `postings` INNER JOIN `medium_info` ON `postings`.`medium_info_id` = `medium_info`.`id` INNER JOIN `accounts` ON `accounts`.`id` = `postings`.`account_id` INNER JOIN `merchants` ON `merchants`.`account_holder_id` = `accounts`.`account_holder_id` INNER JOIN `medium_types` ON `medium_types`.`id` = `accounts`.`medium_type_id` WHERE `merchants`.`account_holder_id` = 230124 AND `medium_info`.`id` = 551678 AND `medium_types`.`id` = 1";
$result = DB::select ( $sql));
但是,当我在 phpMyAdmin 中运行此查询时,它会为我提供带有来自 medium_info 表的“id”的“正确”结果。截图如下:
我想在此处添加通过 DB::select() 查询收到的结果附加了“商家”行,我在查询中没有请求。即使我只是做SELECT `postings`.`posting_timestamp` FROM... 请求,它也会给我这个结果:
Array
(
[0] => stdClass Object
(
[posting_timestamp] => 2020-11-25 23:27:13
[account_holder_id] => 230124
[id] => 230124
[name] => Best Buy
[logo] => /cdn/merchants/230124/logo.png
[description] => <p>When technology meets life, they come together at Best Buy®. Best Buy has the technology that’s fun and functional, from tablets and videogames, to appliances and big screen TVs. Use your gift card at BestBuy.com® or at any US Best Buy store.</p>
[website] => http://www.bestbuy.com
[merchant_code] => BES
[is_premium] => 1
[large_icon] => /cdn/merchants/230124/large_icon.png
[status] => 1
[get_gift_codes_from_root] => 0
[website_is_redemption_url] => 0
[cost_to_program] => 0
[toa_name] =>
)
)
所以很明显,无论我“选择”什么,它都会附加“商家”行。另外,请注意 [id] => 230124 来自“无处”,merchants 表中没有字段 id。在medium_info 表中有一个id 字段,但它应该返回551678 而不是230124,这是merchants 表中字段名称为account_holder_id 的商家ID。
编辑:只是想补充一点,当我在 phpMyAdmin 中运行它时,它不会附加 merchants 数据。
我仍在努力解决这个问题。如果您需要更多信息,我准备提供。这一定与我不理解的 Laravel DB::select 约定有关,因为它在 phpMyAdmin 中有效?任何帮助表示赞赏。
【问题讨论】:
标签: php mysql laravel select phpmyadmin