简体   繁体   English

在两个表上创建UNION ALL后,如何删除/更新另一个表中具有类似ID的特定行?

[英]after making UNION ALL on two tables how could delete / update a specific row that has similer ID in other table?

i hope you understand my problem, on my db i have ord_detail & custom_ord_detail tables 我希望你理解我的问题,在我的数据库上,我有ord_detailcustom_ord_detail

ord_detail ord_detail

-------------------------------------------------
ordID | custID | productID |quantity | discount |
-------------------------------------------------
002   |  1     |     5     |  2      |     0    |
-------------------------------------------------

custom_ord_detail custom_ord_detail

--------------------------------------------------------
ordID | custID | custom_productID |quantity | discount |
--------------------------------------------------------
002   |  1     |     2            |  1      |     0    |
-------------------------------------------------

ordID -> primary key auto_increment (in second table ordID is primary key auto_increment , foreign key refere to table called custom_ord_reply ) ordID >主键auto_increment(在第二个表中ordID是主键auto_increment,外键custom_ord_reply到名为custom_ord_reply表)

custID -> foreign key refer to customer table , productID -> foreign key refer to products table , custom_productID foreign key refer to custom_ord_reply table that the customer request the product in custom_ord_request table. custID >外键引用客户表, productID >外键引用产品表, custom_productID外键引用custom_ord_request表中客户请求产品的custom_ord_reply表。

After we bring the specific product we store it into custom_ord_reply , now we use ord_detail & custom_ord_detail when user added the product into cart and we use union all to show the all products in cart , if the customer want to update the quantity or delete specific product how can do this becouse they will be Duplicate values for the ordID column , how could fix this problem ? 将特定产品带入后,我们将其存储到custom_ord_reply ,现在,当用户将产品添加到购物车中时,我们使用ord_detailcustom_ord_detail ;如果客户要更新数量或删除特定产品,则使用union all来显示购物车中的所有产品怎么做,因为它们将成为ordID列的重复值,如何解决此问题? any idea union all with join 任何想法结合在一起

Viewing your query what I have in mind is add a new attribute/column in both queries as product_type if ordID in both table is unique ord_detail , custom_ord_detail 查看您的查询是我心目中是两个查询中添加一个新属性/列product_type如果ordID在这两个表是唯一ord_detailcustom_ord_detail

'product' as product_type  // 1st query
'custom' as product_type  // 2nd query

If not then 2 new attributes/columns in your query, First product_type as above second add some unique identifier for both queries like for first query include product id and for second query include request id (I assume request id from table custom_ord_request is set to auto increment) 如果不是,那么您的查询中将有2个新属性/列,如上第二个product_type第一个product_type ,为两个查询添加一些唯一标识符,例如第一个查询包括产品ID,第二个查询包括请求ID(我假设来自表custom_ord_request请求ID设置为auto增量)

SELECT p.id as uid, 'product' as product_type, od.ordID , p.prod_name , p.prod_descrip , p.price , od.quantity , od.discount
FROM ord_detail od 
JOIN product p ON od.productID = p.productID
WHERE od.custlD = 1 
UNION ALL 
SELECT rq.id as uid, 'custom' as product_type, cod.ordID , rq.ord_title , rp.description , rp.price , cod.quantity cod.discount
FROM custom_ord_detail cod 
JOIN custom_ord_reply rp ON cod.custom_prod_servID = rp.custom_prod_servID 
JOIN custom_ord_request rq ON rp.ordID = rq.ordID 
WHERE cod.custlD = 1 

This way you could differentiate between your products and custom products for your cart. 这样,您可以区分您的产品和购物车的自定义产品。

Another way would be defining supertype/subtype relationship for your tables. 另一种方法是为表定义超类型/子类型关系。

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

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