|
-
Apr 2nd, 2018, 01:44 AM
#1
[RESOLVED] Combine rows from 2 tables?
I want to be able to generate the following excel report and it appears I need to be able to combine rows from 2 tables, subjects and fees, is this possible? Or any other solution you could recommend?
The database is mysql.
-
Apr 2nd, 2018, 02:50 AM
#2
Re: Combine rows from 2 tables?
Here's my attempt at using UNION, how can I align their rows?
-
Apr 2nd, 2018, 06:56 AM
#3
Re: Combine rows from 2 tables?
That's not what a union is for... if you want the data on the same row, you use a JOIN of some sort... using a common field to join the two tables together.
That said, I'm not sure that's what you want either. It looks like you're trying to dump this data all out in one shot to Excel. That may not be the best way to go. Dump it out in chunks. Do one query to get the class data, dump it to Excel, then get the Fee data and do a second dump to Excel.
-tg
-
Apr 2nd, 2018, 10:00 AM
#4
Re: Combine rows from 2 tables?
Yes I am reconsidering just doing it in steps right now as it looks it may not really be possible to retrieve all those data in one sql. Just in case, I'm leaving this open for others to share their recommendations. Thanks TG!
-
Apr 2nd, 2018, 10:20 AM
#5
Re: Combine rows from 2 tables?
If you have a Row_Number() like function in MySql you could do it like this - here it is in MS SQL
Code:
Create Table #Tbl1 (SomeData varchar(15))
Create Table #Tbl2 (OtherData varchar(15))
Insert into #Tbl1 values ('Test Table 1'),('Test Again 1')
Insert into #Tbl2 values ('Do Table 2'),('Test More 2')
Select X1.*,X2.*
From (Select Row_Number() Over (Order by SomeData) "RowNum",SomeData
From #Tbl1) X1
Left Join (Select Row_Number() Over (Order by OtherData) "RowNum",OtherData
From #Tbl2) X2 on X2.RowNum=X1.RowNum
Drop TAble #Tbl1
Drop Table #Tbl2
And that yields the following
Code:
RowNum SomeData RowNum OtherData
1 Test Again 1 1 Do Table 2
2 Test Table 1 2 Test More 2
I am not handling the situation of having different row numbers in each table. You could deal with that by doing a UNION of the two tables with DISTINCT and not UNION ALL so that becomes the FROM table.
[edit] or maybe just a different join. I've done so many left joins at this point in my life I'm blinded! [/edit]
-
Apr 3rd, 2018, 08:57 PM
#6
Re: Combine rows from 2 tables?
I'm still double checking but this appears to be working, thank you so much for the idea SZ! Sadly, I still need to spread.
Code:
SELECT X1.*,X2.*
FROM (SELECT
@row_number:=CASE
WHEN @customer_no = studentID THEN @row_number + 1
ELSE 1
END AS num,
@customer_no:=studentID AS CustomerNumber, unifastotherfees.*
FROM
`enrollmentsystem`.`unifastotherfees`) AS X1
LEFT JOIN (SELECT
@row_number:=CASE
WHEN @customer_no = studentID THEN @row_number + 1
ELSE 1
END AS num,
@customer_no:=studentID AS CustomerNumber, unifastsubjects.*
FROM
`enrollmentsystem`.`unifastsubjects`) AS X2 ON X2.studentID=X1.studentID AND X2.num=X1.num
-
Apr 4th, 2018, 05:23 AM
#7
Re: [RESOLVED] Combine rows from 2 tables?
What do you mean by "still need to spread"?
-
Apr 5th, 2018, 07:09 PM
#8
Re: [RESOLVED] Combine rows from 2 tables?
-
Apr 6th, 2018, 04:46 AM
#9
Re: [RESOLVED] Combine rows from 2 tables?
Oh - got it! Glad it worked for you!!
-
Apr 7th, 2018, 04:57 AM
#10
Re: [RESOLVED] Combine rows from 2 tables?
Yes it did, I'm really glad you guys are still around to extend some help, it is much appreciated!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|