題目:
銷售表 Sales:
+-------------+-------+
| Column Name | Type |
+-------------+-------+
| sale_id | int |
| product_id | int |
| year | int |
| quantity | int |
| price | int |
+-------------+-------+
(sale_id, year) 是銷售表 Sales 的主鍵.
product_id 是關(guān)聯(lián)到產(chǎn)品表 Product 的外鍵.
注意: price 表示每單位價格
產(chǎn)品表 Product:
+--------------+---------+
| Column Name | Type |
+--------------+---------+
| product_id | int |
| product_name | varchar |
+--------------+---------+
product_id 是表的主鍵.
寫一條SQL 查詢語句獲取 Sales 表中所有產(chǎn)品對應(yīng)的 產(chǎn)品名稱 product_name 以及該產(chǎn)品的所有 售賣年份 year 和 價格 price 攻晒。
查詢結(jié)果中的順序無特定要求家坎。
查詢結(jié)果格式示例如下:
Sales 表:
+---------+------------+------+----------+-------+
| sale_id | product_id | year | quantity | price |
+---------+------------+------+----------+-------+
| 1 | 100 | 2008 | 10 | 5000 |
| 2 | 100 | 2009 | 12 | 5000 |
| 7 | 200 | 2011 | 15 | 9000 |
+---------+------------+------+----------+-------+
Product 表:
+------------+--------------+
| product_id | product_name |
+------------+--------------+
| 100 | Nokia |
| 200 | Apple |
| 300 | Samsung |
+------------+--------------+
Result 表:
+--------------+-------+-------+
| product_name | year | price |
+--------------+-------+-------+
| Nokia | 2008 | 5000 |
| Nokia | 2009 | 5000 |
| Apple | 2011 | 9000 |
+--------------+-------+-------+
java代碼:
# Write your MySQL query statement below
select p.product_name, s.year, s.price from Sales s, Product p where s.product_id = p.product_id;