mysql - 是否可以将此“join”和“as”查询转换为 Laravel PDO 函数?
问题描述
我正在努力使这个 SQL 查询成为 Laravel PDO 查询。有谁知道我该怎么做?(尤其是'AS'
连接中的语句和命名)。
询问:
SELECT models.name as model_name,
models.brand as model_brand,
t.name as trim_name,
t.extra_information as trim_extra_information,
t.price as trim_price,
t.popular as trim_is_popular,
s.type as specification_type,
s.value as specification_value,
o.name as option_name,
o.default as option_default,
o.remaining as option_remaining,
c.name as color_name,
c.hex_code as color_hex_code,
c.price_extra as color_price_extra,
ll.months as lease_length_months,
ll.default as lease_length_default,
ll.price_extra as lease_length_price_extra,
eo.name as extra_option_name,
eo.description as extra_option_description,
eo.price_total as extra_option_price_total,
eo.price_extra as extra_option_price_extra,
m.kilometers as mileage_kilometers,
m.default as mileage_default,
m.price_extra as mileage_price_extra,
m.price_extra_km as mileage_price_extra_km
FROM `models`
INNER JOIN trims t on models.id = t.model_id
INNER JOIN specifications s on t.id = s.trim_id
INNER JOIN options o on t.id = o.trim_id
INNER JOIN colors c on t.id = c.trim_id
INNER JOIN lease_lengths ll on t.id = ll.trim_id
INNER JOIN extra_options eo on ll.id = eo.lease_length_id
INNER JOIN mileages m on ll.id = m.lease_length_id
解决方案
直接使用join()
和select()
方法如下:
Model::join('trims t', 'models.id', '=', 't.model_id')
->join('specifications s', 't.id', '=', 's.trim_id')
...
->select(
'models.name as model_name',
'models.brand as model_brand',
't.name as trim_name',
't.extra_information as trim_extra_information',
't.price as trim_price',
't.popular as trim_is_popular',
...
)
->get()
推荐阅读
- c# - 无法选择元素c#
- ios - 使用 ARkit swift 去除墙壁和改变墙壁颜色
- azure - 应用程序 (Australia.Luis.ai) 已禁用
- jmeter - Hybris 后台使用 Jmeter 和这个 ZK 插件
- ruby - 使用 twitter gem 发送直接消息
- python - 如何测试视图是否能够在数据库中创建新实体?
- azure - 登录 Azure AD B2C 而不重定向到 b2clogin Microsoft 页面
- wso2 - WSO2 API 管理器 - 无法启动服务器
- vba - MS-access 2010 VBA 中的 RecordCount 问题
- node.js - pact-js 消息消费者合同示例不起作用