Option A left joins the "users" table, option B right joins the "orders" table, and option D right joins the "orders" table and filters records where user_id is null. Option C left joins the "users" table and filters records where user_id in the "orders" table is null, which gives the user_id values that exist in the "users" table but not in the "orders" table.
正確答案是:C: `select users.user_id from users left join orders on users.user_id = orders.user_id where orders.user_id is null;`
### 專業(yè)分析:
1. **左連接(LEFT JOIN)**:
- `LEFT JOIN` 會(huì)返回左表(users)中的所有行,即使右表(orders)中沒(méi)有匹配的行。
- 如果右表中沒(méi)有匹配的行,結(jié)果中的右表列將包含 `NULL`。
2. **右連接(RIGHT JOIN)**:
- `RIGHT JOIN` 會(huì)返回右表(orders)中的所有行,即使左表(users)中沒(méi)有匹配的行。
- 如果左表中沒(méi)有匹配的行,結(jié)果中的左表列將包含 `NULL`。
3. **分析選項(xiàng)**:
- **A: `select users.user_id from users left join orders on users.user_id = orders.user_id;`**
- 這個(gè)查詢會(huì)返回所有用戶的 `user_id`,包括那些在 `orders` 表中沒(méi)有匹配的用戶。但它不會(huì)過(guò)濾出那些沒(méi)有訂單的用戶。
- **B: `select users.user_id from users right join orders on users.user_id = orders.user_id;`**
- 這個(gè)查詢會(huì)返回所有有訂單的用戶的 `user_id`,但不會(huì)包含那些沒(méi)有訂單的用戶。
- **C: `select users.user_id from users left join orders on users.user_id = orders.user_id where orders.user_id is null;`**
- 這個(gè)查詢會(huì)返回所有在 `users` 表中存在但在 `orders` 表中不存在的 `user_id`,因?yàn)槲覀冊(cè)?`WHERE` 子句中檢查 `orders.user_id` 是否為 `NULL`。
- **D: `select users.user_id from users right join orders on users.user_id = orders.user_id where users.user_id is null;`**
- 這個(gè)查詢會(huì)返回所有在 `orders` 表中存在但在 `users` 表中不存在的 `user_id`,這與題目要求相反。
因此,正確答案是 C,因?yàn)樗_地返回了在 `users` 表中存在但在 `orders` 表中不存在的 `user_id`。