How to convert this sql into Laravel ORM to write?

WBOY
Release: 2016-08-20 09:04:16
Original
973 people have browsed it

<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>
Copy after login
Copy after login

Mainly because I don’t know how to write the sub-Select section. The reason why I have to write it in one sql is because it involves sorting.

Reply content:

<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>
Copy after login
Copy after login

Mainly because I don’t know how to write the sub-Select section. The reason why I have to write it in one sql is because it involves sorting.

Associate the table relationships. In the query, the algorithm operations in the query can be completed by PHP. You can take a look at this

<code>Laravel的关联,可以使用分组,返回字段和聚合函数
</code>
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!