简体   繁体   中英

PHP-MySQL 3 tables select, update, delete and insert linked with Foreign Key

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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