简体   繁体   English

MySQL包含另一个表中的重复值

[英]MySQL include a repeating value from another table

I have a user profile which stores a number representing a portfolio value (currency). 我有一个用户个人资料,其中存储有代表投资组合价值(货币)的数字。 I have another table which lists recommended investment purchases, with the percentage of the portfolio to be allocated to each fund by risk factor. 我还有另一个表格,其中列出了建议的投资购买,并按风险因素将投资组合的百分比分配给每个基金。 I need to recalculate the dollar value of the fund purchases when ever the percentages change, or when the portfolio value changes. 每当百分比改变或投资组合价值改变时,我都需要重新计算基金购买的美元价值。

I don't want to create a "real" table for every client just to repeat the portfolio value for the calculations. 我不想为每个客户创建一个“真实”表,只是为了重复计算投资组合的价值。 How can I construct a query to select the portfolio value for the client and include it for every row for the calculations to use? 如何构造查询以选择客户的投资组合值,并将其包括在每一行中以供计算使用?

User   PortfolioValue
1      200,000
2      50,000
-----------------------------

Asset     Fund  Risk1Pct   Risk2Pct  
CashE     MM       6%         4%
Fixed     PIMCO    4%         6%
US Equity UEF     50%        75%

Desired Sample Output for User 1 via table generator (visual table, not database table)

Asset     Fund  Risk1Pct  R1Calculation    Risk2Pct  R2Calculation
CashE     MM       6%    (6% * 200,000)     4%      (4% * 200,000)
Fixed     PIMCO    4%    (4% * 200,000)     6%      (6% * 200,000)
US Equity UEF     50%    (50% * 200,000)   75%     (75% * 200,000)

The portfolio value has been added to wp_usermeta, which has that key - value structure. 投资组合的价值已添加到wp_usermeta,它具有该键-值结构。 The portfolio information is in a table called demodb. 项目组合信息位于称为demodb的表中。

The Risk Percentages are in floating point. 风险百分比为浮点数。

Update -------------------------------------------- I have the values I need added into usermeta (user_id, meta_key, meta_value). 更新--------------------------------------------我有值I需要添加到usermeta(user_id,meta_key,meta_value)中。

There are 9 company tables with the investment options for each company (company_id, assetclass, fundname, risk1pct, risk1action, risk2pct, risk2action, risk3pct, risk3action, risk3pct, risk4action). 有9个公司表,每个公司都有投资选项(company_id,资产类别,基金名称,risk1pct,risk1action,risk2pct,risk2action,risk3pct,risk3action,risk3pct,risk4action)。

I created a table userportfolio (user_id, company_id, total401k, companystockamt, loansamt, withdrawalsamt). 我创建了一个表userportfolio(user_id,company_id,total401k,companystockamt,loansamt,drawingsamt)。

I'm trying to get everything connected so that I have out put like user_id, company_id, assetclass, fundname, (risk1pct * total401k), risk1action, (risk2pct * total401k), risk2action, (risk3pct * total401k), risk3action, (risk4pct * total401k), risk4action. 我正在尝试使所有内容连接起来,以便像user_id,company_id,资产类别,基金名称,(risk1pct * total401k),risk1action,(risk2pct * total401k),risk2action,(risk3pct * total401k),risk3action,(risk4pct * total401k),risk4action。

I can't tell whether I actually need the userportfolio table. 我不知道我是否真的需要userportfolio表。 Is this info what was meant by the schema? 这个信息是架构的意思吗?

Assuming your % values are decimals (and that I understand the question), we can do this with a simple join: 假设您的%值是小数(并且我理解这个问题),我们可以通过简单的连接来做到这一点:

Let's call your first table u (users) and your second table ri (recommended investments) 让我们称您的第一个表u (用户)和第二个表ri (推荐投资)

SELECT
    u.User,
    u.PortfolioValue,
    ri.Asset,
    ri.Fund,
    ri.Risk1Pct,
    (u.PortfolioValue * ri.Risk1Pct) AS `R1Calculation`,
    ri.Risk2Pct,
    (u.PortfolioValue * ri.Risk2Pct) AS `R2Calculation`
FROM table1 `u`
JOIN table2 `ri`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM