سؤال

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

enter image description here

Table 2

SELECT nz.`invoice`.`InvoiceID`, nz.`invoice`.`UserID` 
FROM nz.`invoice` 
WHERE nz.`invoice`.`InvoiceID` > 11236 
-- total results 757

enter image description here

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+

enter image description here

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)

fiddle

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).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى dba.stackexchange
scroll top