<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/...