-- 示例代码:使用 INNER JOIN 连接两个表
-- 假设有两个表:`users` 和 `orders`
-- `users` 表包含用户信息,`orders` 表包含订单信息
-- users 表结构:
-- +----+----------+-----------+
-- | id | username | email |
-- +----+----------+-----------+
-- | 1 | Alice | a@abc.com |
-- | 2 | Bob | b@abc.com |
-- | 3 | Charlie | c@abc.com |
-- +----+----------+-----------+
-- orders 表结构:
-- +----+------------+---------+
-- | id | user_id | product |
-- +----+------------+---------+
-- | 1 | 1 | Laptop |
-- | 2 | 1 | Mouse |
-- | 3 | 2 | Keyboard |
-- +----+------------+---------+
-- 使用 INNER JOIN 查询每个用户的订单信息
SELECT users.id, users.username, orders.product
FROM users
INNER JOIN orders ON users.id = orders.user_id;
-- 解释:
-- 上述查询语句会返回所有有订单记录的用户及其对应的订单产品。
-- INNER JOIN 只会返回两个表中匹配的记录,即只有当 `users.id` 和 `orders.user_id` 相等时,才会返回相应的行。
上一篇:mysql截取字符串
下一篇:mysql创建用户
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站