how to show combined results of two tables?
سؤال
I want to show the result as combination of two. Table 1
SELECT au.invoice`.`InvoiceID`, au.invoice`.`UserID`
FROM au.`invoice`
WHERE au.`invoice`.`InvoiceID` > 11126
-- total results 757
Table 2
SELECT nz.`invoice`.`InvoiceID`, nz.`invoice`.`UserID`
FROM nz.`invoice`
WHERE nz.`invoice`.`InvoiceID` > 11236
-- total results 757
I want result combined as table 1 result and table 2 results
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+
What I want it should be the same no of results because I just assigned new invoiceId to table 2, all the users have the same no of invoices.
Can you help me to achieve this result?
Thanks Rajesh
المحلول
Test
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)
If the list of UserID
values in both subqueries is the same with guarantee (although it is unclear why) you may use JOIN ... USING (rownumber, UserID)
. Or even use the enumeration while partitioning by UserID
(using 2 variables in each subquery).
لا تنتمي إلى dba.stackexchange