这就是我正在尝试做的事情。我想看看,根据一定的价格和成本,我的利润是多少。这是一个例子:
SELECT price, cost, profit,
price-5 "price1", cost, price-5-cost "profit1"
FROM table
WHERE product = blue_pants;
这会输出如下数据:
price cost profit price1 cost profit1
我希望它是这样的:
price cost profit
price1 cost profit1
这正是我所追求的数据显示方式。那有意义吗?有没有办法做到这一点。抱歉,我是一个 SQL 菜鸟。
最佳答案
Select price, cost, profit
From table
Where product = 'blue_pants'
Union All
Select price-5, cost, price-5-cost
From table
Where product = 'blue_pants'
这显然会重复行。为了知道哪些行应该出现在第一个查询中,哪些行应该出现在第二个查询中,我们需要知道条件(例如 Where Price-5 > 0
)。
关于MySQL 将每第 N 列作为一个新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16928772/