{ "data": { "question": { "questionId": "1948", "questionFrontendId": "1795", "categoryTitle": "Database", "boundTopicId": 669283, "title": "Rearrange Products Table", "titleSlug": "rearrange-products-table", "content": "
Table: Products
\n+-------------+---------+\n| Column Name | Type |\n+-------------+---------+\n| product_id | int |\n| store1 | int |\n| store2 | int |\n| store3 | int |\n+-------------+---------+\nproduct_id is the primary key for this table.\nEach row in this table indicates the product's price in 3 different stores: store1, store2, and store3.\nIf the product is not available in a store, the price will be null in that store's column.\n\n\n
\n\n
Write an SQL query to rearrange the Products
table so that each row has (product_id, store, price)
. If a product is not available in a store, do not include a row with that product_id
and store
combination in the result table.
Return the result table in any order.
\n\nThe query result format is in the following example.
\n\n\n
Example 1:
\n\n\nInput: \nProducts table:\n+------------+--------+--------+--------+\n| product_id | store1 | store2 | store3 |\n+------------+--------+--------+--------+\n| 0 | 95 | 100 | 105 |\n| 1 | 70 | null | 80 |\n+------------+--------+--------+--------+\nOutput: \n+------------+--------+-------+\n| product_id | store | price |\n+------------+--------+-------+\n| 0 | store1 | 95 |\n| 0 | store2 | 100 |\n| 0 | store3 | 105 |\n| 1 | store1 | 70 |\n| 1 | store3 | 80 |\n+------------+--------+-------+\nExplanation: \nProduct 0 is available in all three stores with prices 95, 100, and 105 respectively.\nProduct 1 is available in store1 with price 70 and store3 with price 80. The product is not available in store2.\n\n", "translatedTitle": "每个产品在不同商店的价格", "translatedContent": "
表:Products
\n+-------------+---------+\n| Column Name | Type |\n+-------------+---------+\n| product_id | int |\n| store1 | int |\n| store2 | int |\n| store3 | int |\n+-------------+---------+\n这张表的主键是product_id(产品Id)。\n每行存储了这一产品在不同商店store1, store2, store3的价格。\n如果这一产品在商店里没有出售,则值将为null。\n\n\n
\n\n
请你重构 Products
表,查询每个产品在不同商店的价格,使得输出的格式变为(product_id, store, price)
。如果这一产品在商店里没有出售,则不输出这一行。
输出结果表中的 顺序不作要求 。
\n\n查询输出格式请参考下面示例。
\n\n\n\n
示例 1:
\n\n\n输入:\nProducts table:\n+------------+--------+--------+--------+\n| product_id | store1 | store2 | store3 |\n+------------+--------+--------+--------+\n| 0 | 95 | 100 | 105 |\n| 1 | 70 | null | 80 |\n+------------+--------+--------+--------+\n输出:\n+------------+--------+-------+\n| product_id | store | price |\n+------------+--------+-------+\n| 0 | store1 | 95 |\n| 0 | store2 | 100 |\n| 0 | store3 | 105 |\n| 1 | store1 | 70 |\n| 1 | store3 | 80 |\n+------------+--------+-------+\n解释:\n产品0在store1,store2,store3的价格分别为95,100,105。\n产品1在store1,store3的价格分别为70,80。在store2无法买到。\n", "isPaidOnly": false, "difficulty": "Easy", "likes": 13, "dislikes": 0, "isLiked": null, "similarQuestions": "[]", "contributors": [], "langToValidPlayground": "{\"cpp\": false, \"java\": false, \"python\": false, \"python3\": false, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"ruby\": false, \"bash\": false, \"swift\": false, \"golang\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"kotlin\": false, \"rust\": false, \"php\": false, \"typescript\": false, \"racket\": false, \"erlang\": false, \"elixir\": false}", "topicTags": [ { "name": "Database", "slug": "database", "translatedName": "数据库", "__typename": "TopicTagNode" } ], "companyTagStats": null, "codeSnippets": [ { "lang": "MySQL", "langSlug": "mysql", "code": "# Write your MySQL query statement below", "__typename": "CodeSnippetNode" }, { "lang": "MS SQL Server", "langSlug": "mssql", "code": "/* Write your T-SQL query statement below */", "__typename": "CodeSnippetNode" }, { "lang": "Oracle", "langSlug": "oraclesql", "code": "/* Write your PL/SQL query statement below */", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"2.9K\", \"totalSubmission\": \"3.8K\", \"totalAcceptedRaw\": 2944, \"totalSubmissionRaw\": 3802, \"acRate\": \"77.4%\"}", "hints": [], "solution": null, "status": null, "sampleTestCase": "{\"headers\":{\"Products\":[\"product_id\",\"store1\",\"store2\",\"store3\"]},\"rows\":{\"Products\":[[0, 95, 100, 105], [1, 70, null, 80]]}}", "metaData": "{\n \"mysql\": [\n \"Create table If Not Exists Products (product_id int, store1 int, store2 int, store3 int)\"\n ],\n \"mssql\": [\n \"Create table Products (product_id int, store1 int, store2 int, store3 int)\"\n ],\n \"oraclesql\": [\n \"Create table Products (product_id int, store1 int, store2 int, store3 int)\"\n ],\n \"database\": true\n}", "judgerAvailable": true, "judgeType": "large", "mysqlSchemas": [ "Create table If Not Exists Products (product_id int, store1 int, store2 int, store3 int)", "Truncate table Products", "insert into Products (product_id, store1, store2, store3) values ('0', '95', '100', '105')", "insert into Products (product_id, store1, store2, store3) values ('1', '70', 'None', '80')" ], "enableRunCode": true, "envInfo": "{\"mysql\":[\"MySQL\",\"
\\u7248\\u672c\\uff1a mssql server 2019.<\\/p>\"],\"oraclesql\":[\"Oracle\",\" Oracle Sql 11.2.<\\/p>\"]}",
"book": null,
"isSubscribed": false,
"isDailyQuestion": false,
"dailyRecordStatus": null,
"editorType": "CKEDITOR",
"ugcQuestionId": null,
"style": "LEETCODE",
"exampleTestcases": "{\"headers\":{\"Products\":[\"product_id\",\"store1\",\"store2\",\"store3\"]},\"rows\":{\"Products\":[[0, 95, 100, 105], [1, 70, null, 80]]}}",
"__typename": "QuestionNode"
}
}
}MySQL 8.0<\\/code><\\/p>\"],\"mssql\":[\"MS SQL Server\",\"