SQL-如何将一个列合并并完全联接另一列?

时间:2019-04-08 15:51:56

标签: sql

我有2张桌子:

  1. 价格-主键class StartPage(tk.Frame): testListOut = [] def __init__(self, parent, controller): tk.Frame.__init__(self, parent) self.testList = [] self.canvas = tk.Canvas(self, borderwidth=2, relief="sunken") self.frame = tk.Frame(self.canvas) self.scrollbar = tk.Scrollbar(self.canvas, orient="vertical", command=self.canvas.yview) self.canvas.configure(yscrollcommand=self.scrollbar.set) self.scrollbar.pack(side="right", fill="y") self.canvas.pack(side="top", fill="both" ,expand="true") self.canvas.create_window((4, 4), window=self.frame, anchor="nw",tags="self.frame") self.frame.bind("<Configure>", self.onFrameConfigure) self.populateCheckBox(lNames) def populateCheckBox(self ,testList): self.checks = [] self.vars = [] label = tk.Label(self.frame , width=3, borderwidth="1") label.pack() for element in testList: var = tk.BooleanVar() var.set(False) checkbox = tk.Checkbutton(label, relief="flat") checkbox["text"] = element checkbox["command"] = lambda: self.handler(testList) checkbox["variable"] = var checkbox.grid(sticky="w", padx="10", pady="5") self.checks.append(checkbox) self.vars.append(var) def onFrameConfigure(self, event): self.canvas.configure(scrollregion=self.canvas.bbox("all")) ID
  2. DiscountPrices -主键priceID

价格

discountPrice

折扣价格

ID | price  
1  | 15  
2  | 29  
3  | 21 

我需要获得:

ID | discountPrices  
1  | 12  
3  | 18   
4  | 22

1。要获取总ID列表,我可以使用:

ID | price | discountPrice  
1  |  15   | 12    
2  |  29   | NULL  
3  |  21   | 18  
4  |  NULL | 22  

2。要获得价格,可以使用DiscountPrice:

SELECT ID FROM Prices
UNION
SELECT ID FROM DiscountPrices

至此,我已经停下来了。你能给我一些提示吗? :)

1 个答案:

答案 0 :(得分:1)

FULL JOIN更改为FULL OUTER JOIN将解决您的问题:

SELECT COALESCE(p.ID, dp.id) AS ID, p.price, dp.discountPrice 
FROM Prices p FULL OUTER JOIN 
     DiscountPrices  dp
     ON p.ID = dp.ID;

只有JOIN被视为INNER JOIN,但您需要OUTER JOIN