gpt4 book ai didi

php - 计算的 mysql 变量不显示在 PHP 结果集中

转载 作者:行者123 更新时间:2023-11-30 21:54:01 25 4
gpt4 key购买 nike

当我在 Sequel Pro 中运行以下查询时,我得到了所有列的结果

select distinct
@company_id := a.company_id as company,
@current_date := max(stock_date) as currentdate,
@other_date := date_sub(@current_date, interval 0 day) as other_date,
@shares_bought := (select sum(volume) from my_portfolio where company_id=@company_id and posted_by = '5977479698B58' and trans_type='BUY') as shares_bought,
@shares_sold := (select IF(sum(volume) IS NULL, 0, sum(volume)) from my_portfolio where company_id=@company_id and posted_by = '5977479698B58' and trans_type='SELL') as shares_sold,
@shares_held := (@shares_bought-@shares_sold) as shares_held,
@current_price := (select close from stock_prices where company_id=@company_id and stock_date <= @other_date order by stock_date desc limit 1) as current_price,
(@current_price*@shares_held) as market_value
from my_portfolio a
inner join stock_prices b on a.company_id = b.company_id
group by company

但是,我的 PHP var_dump() 显示某些实际具有值的列的空值。

Image: var_dump from PHP

Image: results of the same query from Sequel Pro

最佳答案

如果 other_date 总是与 currentdate 相同,那么试试这样:

SELECT company, currentdate, currentdate AS other_date, shares_bought, shares_sold,
shares_held, current_price, current_price * shares_held AS market_value
FROM ( SELECT company, currentdate, currentdate AS other date, shares_bought, shares_sold,
shares_bought - shares_sold AS shares_held,
( SELECT close FROM stock_prices WHERE company_id = q.company_id
AND stock_date <= currentdate ORDER BY stock_date DESC
LIMIT 1 ) AS current_price
FROM ( SELECT a.company_id AS company, MAX(stock_date) AS currentdate,
COALESCE( SUM( b.volume ), 0 ) AS shares_bought,
COALESCE( SUM( s.volume ), 0 ) AS shares_sold
FROM stock_prices a
JOIN my_portfolio b ON b.company_id = a.company_id AND b.trans_type = 'BUY'
AND b.posted_by = '5977479698B58'
JOIN my_portfolio s ON s.company_id = a.company_id AND s.trans_type = 'SELL'
AND s.posted_by = '5977479698B58'
GROUP BY a.company_id
) q
) w

我很想使用 CTE(WITH 子句),但它只适用于 MySQL 8....

关于php - 计算的 mysql 变量不显示在 PHP 结果集中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46010599/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com