简体   繁体   English

从mysql中的不同表中获取2个主键

[英]To get 2 primary keys from different tables in mysql

I'm doing my final year project and planning to do comment feature on my system.我正在做我最后一年的项目,并计划在我的系统上做评论功能。 The customer that have purchased the meal only can comment or give feedback about that particular meal.已购买餐食的顾客只能对特定餐食发表评论或提供反馈。 So, I need to get the Meal_ID and Cust_ID from table 1 and table 2 in order to keep track who's comment on what meal.因此,我需要从表 1 和表 2 中获取 Meal_ID 和 Cust_ID,以便跟踪谁对哪顿饭的评论。 I just want to ask is it possible to select multiple primary keys from different tables in one sql query?我只是想问一下是否可以在一个sql查询中从不同的表中选择多个主键? Thanks in advance!提前致谢!

I have 3 table as shown below.我有 3 个表,如下所示。

在此处输入图片说明

Foreign Key:外键:

Example:例子:

CREATE TABLE Orders
(
O_Id int NOT NULL,
OrderNo int NOT NULL,
P_Id int,
PRIMARY KEY (O_Id),
FOREIGN KEY (P_Id) REFERENCES Persons(P_Id)
)

Description: The following SQL creates a FOREIGN KEY on the "P_Id" column when the "Orders" table is created:说明:以下 SQL 在创建“Orders”表时在“P_Id”列上创建 FOREIGN KEY:

Fetching Data:获取数据:

Ya it it possible to select two Primary keys from a single select statement with the help of JOIN Query so that you can combine both the tables and you can get the result with the help of single query.是的,可以在 JOIN Query 的帮助下从单个 select 语句中选择两个主键,以便您可以组合两个表,并且可以在单个查询的帮助下获得结果。

There are different types of Join available and you can use any such method:有不同类型的 Join 可用,您可以使用任何此类方法:

  1. JOIN加入
  2. Left JOIN左加入
  3. Right JOIN右加入

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM