欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

sql语句转换成Laravel ORM的问题。

程序员文章站 2022-05-23 20:37:30
...
SELECT
    e.external_account_id,
    e.trade_type,
  e.stock_code,
    e.name,
    e.stock_name,
    SUM(e.trade_price * e.trade_count) / SUM(e.trade_count) AS trade_price,
    SUM(e.trade_count) AS trade_count
FROM
    (
        SELECT
            a.id,
            a.external_account_id,
            a.internal_account_id,
            a.trade_price,
            a.trade_count,
            b.stock_code,
            b.trade_type,
            c.name as stock_name,
            d.name
        FROM
            trades a,
            orders b,
            stocks c,
            external_accounts d
        WHERE
            a.order_id = b.id
        AND b.stock_code = c.code
        AND a.external_account_id = 29
        AND d.id = 29
    ) AS e
GROUP BY e.stock_code, e.trade_type

这段sql转换成ORM应该怎么写啊= =。
Laravel用的不多,求高玩赐教。。

踩我的朋友们,嫌我问的问题low?问的low你来答啊。答不出来还踩,什么心态。?

回复内容:

SELECT
    e.external_account_id,
    e.trade_type,
  e.stock_code,
    e.name,
    e.stock_name,
    SUM(e.trade_price * e.trade_count) / SUM(e.trade_count) AS trade_price,
    SUM(e.trade_count) AS trade_count
FROM
    (
        SELECT
            a.id,
            a.external_account_id,
            a.internal_account_id,
            a.trade_price,
            a.trade_count,
            b.stock_code,
            b.trade_type,
            c.name as stock_name,
            d.name
        FROM
            trades a,
            orders b,
            stocks c,
            external_accounts d
        WHERE
            a.order_id = b.id
        AND b.stock_code = c.code
        AND a.external_account_id = 29
        AND d.id = 29
    ) AS e
GROUP BY e.stock_code, e.trade_type

这段sql转换成ORM应该怎么写啊= =。
Laravel用的不多,求高玩赐教。。

踩我的朋友们,嫌我问的问题low?问的low你来答啊。答不出来还踩,什么心态。?

转换成ORM增加了复杂性,降低了sql语句可读性,还不如直接用DB::select()来执行sql。

复杂的sql没必要改成orm

相关标签: php laravel