Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 选择查询以两次提取同一记录_Sql_Select_Duplicates_Default_Record - Fatal编程技术网

Sql 选择查询以两次提取同一记录

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

我想使用select查询两次拉取相同的记录,但第二次拉取其中一个字段的值不同

因此,我有一个select查询,它当前提取数据并显示以下内容,例如:

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;

哦,我的天,其实很简单,谢谢你,工会都成功了!