Sql 选择查询以两次提取同一记录
我想使用select查询两次拉取相同的记录,但第二次拉取其中一个字段的值不同 因此,我有一个select查询,它当前提取数据并显示以下内容,例如:Sql 选择查询以两次提取同一记录,sql,select,duplicates,default,record,Sql,Select,Duplicates,Default,Record,我想使用select查询两次拉取相同的记录,但第二次拉取其中一个字段的值不同 因此,我有一个select查询,它当前提取数据并显示以下内容,例如: ProductCode Description Price Currency 123 Product1 200 CAD 现在我想使用select查询第二次拉取相同的记录,但我想使用USD,而不是CAD,因此它应该显示: ProductCode Description Price Curr
ProductCode Description Price Currency
123 Product1 200 CAD
现在我想使用select查询第二次拉取相同的记录,但我想使用USD,而不是CAD,因此它应该显示:
ProductCode Description Price Currency
123 Product1 200 CAD
123 Product1 200 USD
“CAD”现在在select查询中是硬编码的,我希望这是有意义的,例如,现在的select查询是:
select productcode,description,price, 'CAD' as Currency from product_table
工会怎么样
SELECT ProductCode, Description, price_in_cad as Price, 'CAD' as Currency FROM product_table
UNION ALL
SELECT ProductCode, Description, price_in_usd as Price, 'USD' as Currency FROM product_table;
或者,如果要对其进行排序或筛选,则可以从中进行子查询:
SELECT result.* FROM (
SELECT ProductCode, Description, price_in_cad as Price, 'CAD' as Currency FROM product_table
UNION ALL
SELECT ProductCode, Description, price_in_usd as Price, 'USD' as Currency FROM product_table
) result ORDER BY Price;
哦,我的天,其实很简单,谢谢你,工会都成功了!