JSP & MySql: display 1 resultset from multiple tables.

hi guys,
i'm preparing a few reports for my manager in food business.
i have a few tables:
purchase-(id, code,date,doNo,amount)<--related to supplier

 suppliers & purchase
1) i need to display the list of suppliers and the amount of purchase we made for certain month.
i can group them by code but how can i display them accordingly(sorted by name)?

 foodtrans & papertrans
2) there's 2 types of transfer, In and Out. i need to get the total amount of food and paper transfer according to their types, also for a certain month.
<td>Transfer In</td>
<td>Transfer Out</td>

all this into 1 dynamic table.
Can i use only 1 query statement?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hi, yapsing!

1. Assuming that you have a supplier_id field in your purchases table (as a foreign key to maintain reference):

// create database connection before...
String query = "SELECT name,SUM(amount) as total FROM suppliers INNER JOIN purchases ON suppliers.id=purchases.supplier_id " +
      "GROUP BY name ORDER BY name";
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(query);
while (rs.next()) {
   out.println ("<tr>");
   String name = rs.getString("name");
   int amount = rs.getInt("total");
   out.println ("<td>" + name + "</td>");
   out.println ("<td>" + amount + "</td>");
   out.println ("</tr>");

For the secont part, I guess it will look similar.

String query = "SELECT t1.type AS transtype,famount,pamount FROM " +
   "(SELECT type,sum(amount) AS famount FROM foodtrans GROUP BY type) AS t1 FULL OUTER JOIN " +
   "(SELECT type,sum(amount) AS pamount FROM papertrans GROUP BY type) AS t2 ON t1.type=t2.type";
while (rs.next()) {
   String type = rs.getString("transtype");
   int famount = rs.getInt(famount);
   int pamount = rs.getInt(pamount);

Good luck!
yapsingAuthor Commented:
oh its u again, glad to u see u here.
Give me some time to try it out, say, 1/2 hr. u using MSN messenger or Yahoo Messenger?
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

yapsingAuthor Commented:
TYPE is a MySQL keyword. gotta change the structures abit
yapsingAuthor Commented:
i tried keying all test data but it return empty set any idea y?
yapsingAuthor Commented:
You have an error in your SQL syntax near 'SELECT trans,sum(amount) AS famount FROM foodtrans GROUP BY trans) AS t1 FULL OU' at line 1

1. Which of the two queries returned empty set?
2. I guess the syntax in the second query is not good specifically for MySQL (tested on PostgreSQL). MySQL doesn't support FULL OUTER JOIN (amazing!). So you'll need to use some workaroung like this:

SELECT t1.type AS transtype,famount,pamount FROM
(SELECT type,sum(amount) AS famount FROM foodtrans GROUP BY type) AS t1 LEFT OUTER JOIN
(SELECT type,sum(amount) AS pamount FROM papertrans GROUP BY type) AS t2 ON t1.type=t2.type
(SELECT type,sum(amount) AS famount FROM foodtrans GROUP BY type) AS t3 RIGHT OUTER JOIN
(SELECT type,sum(amount) AS pamount FROM papertrans GROUP BY type) AS t4 ON t3.type=t4.type
yapsingAuthor Commented:
1) ignore it, my mistake
2) papertrans and foodtrans are independent of each other maybe we can solve this by using another approach i.e
select sum(foodtrans.amount) as famountIn,sum(papertrans.amount) as pamountIn from foodtrans,papertrans where foodtrans.type=papertrans.type and foodtrans.type='in' and papertrans.type='in'

float transIn_amt = famountIn + pamountIn;

select sum(foodtrans.amount) as famountOut,sum(papertrans.amount) as pamountOut from foodtrans,papertrans where foodtrans.type=papertrans.type and foodtrans.type='out' and papertrans.type='out'

float transOut_amt = famountOut + pamountOut;

i know this is very amatuer but what else can i do?????
no, you'll get wrong data:

'in'   10
'in'    20

'in' 100

select papertrans.type,papertrans.amount,foodtrans.amount from papertrans,foodtrans where papertrans.type='in' and foodtrans.type='in':

type papertrans.amount foodtrans.amount
'in'                     10                      100
'in'                     20                      100

And your sums will be 30 and 200 (not 100).

If you need foodtrans sum and papertrans sum in the same result set, you'll have to process subquery. Isn't it working? If so, I suggest getting the sums separately:

SELECT type,sum(amount) FROM papertrans GROUP BY type
SELECT type,sum(amount) FROM foodtrans GROUP BY type

... and processing two resultsets in your JSP. It's not that difficult.
yeah, I just re-readyour code... If you do

float transIn_amt = famountIn + pamountIn;

why not getting famountIn and pamountIn separately?

ResultSet rs1 = st.executeQuery("SELECT type,sum(amount) as pamount FROM papertrans GROUP BY type");
double pamountIn, pamountOut;
while (rs.next()) {
   if (rs.getString("type").equals("in"))
      pamountIn = rs.getFloat(pamount);
      pamountOut = rs.getFloat(pamount);
ResultSet rs2 = st.executeQuery("SELECT type,sum(amount) as famount FROM foodtrans GROUP BY type");
double famountIn, famountOut;
while (rs.next()) {
   if (rs.getString("type").equals("in"))
      famountIn = rs.getFloat(famount);
      famountOut = rs.getFloat(famount);

Is it ok?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
yapsingAuthor Commented:
yeah i think i'll work like this, less complicated that way. i was thinking i could only use 1 resultset but 2 is better than none. easier to manage
You can do it with one resultset, no doubt of it, but you'll have to use subqueries. I've tested the solution on Postgres, it works fine...

Good luck in everything!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.