INNER JOIN
Joining two tables
To use data from both tables at once, you join them. An INNER JOIN pairs up rows that match on a key — here, the order's customer_id with the customer's id:
When a column name could come from either table, write table.column so it is clear.
2つのテーブルの結合
両方のテーブルのデータ одновременноに使用する際、それらを結合します。INNER JOIN はキーに基づいて一致する行をペアにします — ここでは、注文の customer_id と顧客の id です:
SELECT customer.name, orders.total
FROM customer
INNER JOIN orders ON customer.id = orders.customer_id;
カラム名がどちらのテーブルから来的るか不明確な場合は、table.column を明記して明確にします。
Only matching rows survive
An INNER JOIN keeps a row only when the ON condition finds a match in the other table.
In our shop, Ben and Dan have no orders, so they do not appear in the join — there is nothing to pair them with. Ada and Cara, who do have orders, appear once for each of their orders.
一致する行のみが残る
INNER JOIN は、ON の条件が他方のテーブルで一致する行を見つけた場合に限り、行を保持します。
在我们的商店中,Ben和Dan没有订单,因此他们不会出现在连接结果中——因为没有可以与之配对的记录。而Ada和Cara有订单,所以他们的订单数量有多少,就会在结果中出现多少次。
Common mistakes
- Match on the key with
ON; a missingONpairs every row with every row. - INNER JOIN drops rows with no match on the other side.
よくあるミス
ONのキーに一致する行を抽出し、ONが欠落している場合、すべての行が他のすべての行とペアになります。- INNER JOIN は、片方に一致する行がない行を除外します。
INNER JOIN matches keys · INNER JOIN はキーを照合する
Rows from two tables combine where the key matches. · 2つのテーブルの行は、キーが一致する場所で結合される。
List each customer's name next to the total of each of their orders. Join customer to orders on the key, ordered by orders.id. · 各顧客のnameを、その各注文のtotalの隣に表示する。キーに基づいてcustomer をorders にジョインし、orders.id で並べ替える。
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
Show the customer name and order total for orders of 25 or more, largest total first. · 顧客の name と注文の total を、合計金額が最大となるように、25 以上の注文について表示する。
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。