PHP-MySQL 3表选择、更新、删除和插入与外键链接

发布于 2024-10-26 20:11:03 字数 761 浏览 4 评论 0原文

我在 MySQL 中与外键链接的 3 个 InnoDB 表上遇到问题,我的表是:

Table1 name - products

PID(PK, bigint, auto inc), 
CATALOG_NO, 
PRODUCT_NAME, 
COMPOSITION, 
SIZE, 
PRICE, 
SUBCAT_ID(foreign Key, index, bigint, keyname-FK_products_1)

Table2 name - subcategory

SUBCAT_ID(PKey, bigint, auto inc),
SUBCATEGORY_NAME, CAT_ID(fkey, bigint)


Table3 name - category

CAT_ID(Pkey, bigint, auot inc),
CAT_NAME

1.什么是 SELECT 的正确查询并通过连接 3 个表列出数据,其结果应显示为:

ProductsPID, CATALOG_NO, PRODUCT_NAME, COMPO, SIZE, PRICE, SUBCATEGORY_NAME, CAT_NAME

2.正确的方法是什么通过一个表单使用一个查询来更新和删除上述连接记录?

3.是否可以使用包含产品名称(输入类型=文本)、目录号(输入类型=文本)、成分(输入类型=文本)、尺寸(输入类型=文本),价格(输入类型=文本),选择子类别(选择带选项的类型字段),类别(选择带选项的类型字段)如果是,那么如何?

请帮忙,很紧急。

I have a problem with 3 InnoDB tables in MySQL linked with a foreign key, My tables are:

Table1 name - products

PID(PK, bigint, auto inc), 
CATALOG_NO, 
PRODUCT_NAME, 
COMPOSITION, 
SIZE, 
PRICE, 
SUBCAT_ID(foreign Key, index, bigint, keyname-FK_products_1)

Table2 name - subcategory

SUBCAT_ID(PKey, bigint, auto inc),
SUBCATEGORY_NAME, CAT_ID(fkey, bigint)


Table3 name - category

CAT_ID(Pkey, bigint, auot inc),
CAT_NAME

1.What is the right query to SELECT and list the data by joining 3 tables which should display result as:

ProductsPID, CATALOG_NO, PRODUCT_NAME, COMPO, SIZE, PRICE, SUBCATEGORY_NAME, CAT_NAME

2.What is the correct way to UPDATE and DELETE the above joined records by using one single query through one single form?

3.Is it possible to insert the records also through single query by using single html form containing fields such as product name (input type=text), catalog no (input type=text), composition (input type=text), size (input type=text), price (input type=text), Choose subcategory (select type field with options), category (select type field with options) if yes then how?

PLEASE HELP, ITS URGENT.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流心雨 2024-11-02 20:11:03
SELECT
  p.PID AS ProductsPID,
  p.CATALOG_NO, 
  p.PRODUCT_NAME, 
  p.COMPOSITION AS COMPO, 
  p.SIZE, 
  p.PRICE, 
  s.SUBCATEGORY_NAME,
  c.CAT_NAME
FROM products p
  INNER JOIN subcategory s
    ON p.SUBCAT_ID = s.SUBCAT_ID
  INNER JOIN category c
    ON s.CAT_ID = c.CAT_ID

在某些情况下,更新和删除也可以使用联接(不是在外键上),但您可能只需要更改产品表。插入必须位于各个表上。

SELECT
  p.PID AS ProductsPID,
  p.CATALOG_NO, 
  p.PRODUCT_NAME, 
  p.COMPOSITION AS COMPO, 
  p.SIZE, 
  p.PRICE, 
  s.SUBCATEGORY_NAME,
  c.CAT_NAME
FROM products p
  INNER JOIN subcategory s
    ON p.SUBCAT_ID = s.SUBCAT_ID
  INNER JOIN category c
    ON s.CAT_ID = c.CAT_ID

update and delete can also use join in some cases (not on foreignkeys) but you probably only need to change the product table. insert has to be on the individual tables.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文