這段sql如何轉換成Laravel ORM來寫?

WBOY
發布: 2016-08-20 09:04:16
原創
1019 人瀏覽過

<code>SELECT
    ea.id,
    ea.name,
    ea.capital_balance,
    ea.total_balance,
    ea.available_balance,
    ea.cashable_balance,
    ea.market_value as old_market_value,
    mv.market_value,
    ea.total_balance + mv.market_value as total_value,
    ea.brokerage_rate,
    ea.state,
    ea.description,
    ea.created_at
FROM
    external_accounts ea,
    (
        SELECT
            c.external_account_id,
            SUM(c.amount) AS market_value
        FROM
            (
                SELECT
                    a.external_account_id,
                    a.total_count * b.price AS amount
                FROM
                    main_inventories a
                JOIN stocks b ON a.stock_code = b.code
            ) AS c
        GROUP BY
            c.external_account_id
    ) AS mv
WHERE
    ea.id = mv.external_account_id
</code>
登入後複製
登入後複製

主要是子Select那一段不知道怎麼寫,之所以非要一條sql寫完的原因就是因為牽涉到排序。

回覆內容:

<code>SELECT
    ea.id,
    ea.name,
    ea.capital_balance,
    ea.total_balance,
    ea.available_balance,
    ea.cashable_balance,
    ea.market_value as old_market_value,
    mv.market_value,
    ea.total_balance + mv.market_value as total_value,
    ea.brokerage_rate,
    ea.state,
    ea.description,
    ea.created_at
FROM
    external_accounts ea,
    (
        SELECT
            c.external_account_id,
            SUM(c.amount) AS market_value
        FROM
            (
                SELECT
                    a.external_account_id,
                    a.total_count * b.price AS amount
                FROM
                    main_inventories a
                JOIN stocks b ON a.stock_code = b.code
            ) AS c
        GROUP BY
            c.external_account_id
    ) AS mv
WHERE
    ea.id = mv.external_account_id
</code>
登入後複製
登入後複製

主要是子Select那一段不知道怎麼寫,之所以非要一條sql寫完的原因就是因為牽涉到排序。

將表格關係都關聯起來,在查詢,查詢裡的演算法運算可以交由PHP完成,你可以看下這個

<code>Laravel的关联,可以使用分组,返回字段和聚合函数
</code>
登入後複製

http://www.kancloud.cn/baidu/...

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板