Mysql
如何顯示兩個表的組合結果?
我想將結果顯示為兩者的組合。表格1
SELECT au.invoice`.`InvoiceID`, au.invoice`.`UserID` FROM au.`invoice` WHERE au.`invoice`.`InvoiceID` > 11126 -- total results 757
表 2
SELECT nz.`invoice`.`InvoiceID`, nz.`invoice`.`UserID` FROM nz.`invoice` WHERE nz.`invoice`.`InvoiceID` > 11236 -- total results 757
我希望將結果組合為表 1 結果和表 2 結果
SELECT au.invoice`.`InvoiceID`, au.invoice`.`UserID`, nz.`invoice`.`InvoiceID`, nz.`invoice`.`UserID` FROM au_._invoice` JOIN nz.`invoice` ON nz.`invoice`.`UserID` = au.invoice`.`UserID` WHERE au.invoice`.`InvoiceID` > 11126 AND nz.`invoice`.`InvoiceID` > 11236 -- total results 1000+
我想要的結果應該是相同的結果,因為我剛剛將新的 invoiceId 分配給表 2,所有使用者的發票數量都相同。
你能幫我實現這個結果嗎?
謝謝拉傑什
測試
SELECT t1.InvoiceID, t1.UserID, t2.InvoiceID, t2.UserID FROM ( SELECT InvoiceID, UserID, @num1 := @num1 + 1 rownumber FROM au.invoice, (SELECT @num1 := 0) variable WHERE InvoiceID > 1234 ORDER BY UserId ) t1 JOIN ( SELECT InvoiceID, UserID, @num2 := @num2 + 1 rownumber FROM nz.invoice, (SELECT @num2 := 0) variable WHERE InvoiceID > 1234 ORDER BY UserId ) t2 USING (rownumber)
如果
UserID
兩個子查詢中的值列表與保證相同(儘管不清楚原因),您可以使用JOIN ... USING (rownumber, UserID)
. 甚至在分區時使用列舉UserID
(在每個子查詢中使用 2 個變數)。