簡體   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?

我希望你理解我的問題,在我的數據庫上,我有ord_detailcustom_ord_detail

ord_detail

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

custom_ord_detail

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

ordID >主鍵auto_increment(在第二個表中ordID是主鍵auto_increment,外鍵custom_ord_reply到名為custom_ord_reply表)

custID >外鍵引用客戶表, productID >外鍵引用產品表, custom_productID外鍵引用custom_ord_request表中客戶請求產品的custom_ord_reply表。

將特定產品帶入后,我們將其存儲到custom_ord_reply ,現在,當用戶將產品添加到購物車中時,我們使用ord_detailcustom_ord_detail ;如果客戶要更新數量或刪除特定產品,則使用union all來顯示購物車中的所有產品怎么做,因為它們將成為ordID列的重復值,如何解決此問題? 任何想法結合在一起

查看您的查詢是我心目中是兩個查詢中添加一個新屬性/列product_type如果ordID在這兩個表是唯一ord_detailcustom_ord_detail

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

如果不是,那么您的查詢中將有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 

這樣,您可以區分您的產品和購物車的自定義產品。

另一種方法是為表定義超類型/子類型關系。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM