gpt4 book ai didi

mysql - 如何参数化 JOIN?

转载 作者:行者123 更新时间:2023-11-29 13:37:00 25 4
gpt4 key购买 nike

给出下表

mysql> describe customers;
+-------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+----------------+
| customer_id | int(11) | NO | PRI | NULL | auto_increment |
| login | varchar(16) | NO | | NULL | |
| password | varchar(40) | NO | | NULL | |
| name | varchar(32) | NO | UNI | NULL | |
| address | varchar(64) | NO | | NULL | |
| contact | varchar(32) | NO | | NULL | |
+-------------+-------------+------+-----+---------+----------------+
6 rows in set (0.02 sec)

mysql> describe orders;
+-----------------+-------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+-------------+------+-----+-------------------+----------------+
| customer_id | int(11) | NO | MUL | NULL | |
| order_id | int(11) | NO | PRI | NULL | auto_increment |
| order_timeStamp | timestamp | NO | | CURRENT_TIMESTAMP | |
| item | varchar(16) | NO | | NULL | |
| price | double | NO | | NULL | |
+-----------------+-------------+------+-----+-------------------+----------------+
5 rows in set (0.04 sec)

我想要获取与参数化查询匹配的所有订单行(我的代码将传入 customer_id),以及客户表中的客户名称。

我该怎么做?

最佳答案

JOIN两个表,并将条件放在查询末尾的 WHERE 子句中:

SELECT
o.order_id,
o.order_timestamp,
o.item,
o.price,
c.name,
c.address,
c.contact
FROM Orders AS o
INNER JOIN Customers AS c ON o.customer_id = c.customer_id
WHERE o.customer_id = ?

关于mysql - 如何参数化 JOIN?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18608451/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com