Solved

Mysql

Posted on 2013-11-11
7
527 Views
Last Modified: 2013-11-11
I am stepping through a mysql table called trans where the date field matches a date I enter, and matching the sellerId field in trans table with a field called number and a field called date in a table called seller.  I am printing info from the trans table as well as the name field from the seller table.  I need to JOIN another table to the query so I can get a name from another table called buyer.  I will need to key on the buyer table fields of buyerID and date.  I have included below, the code I have at this point.  i get an execution syntax error that says:

systax to use near AS b ON t.buyerID = b.number where t.date='2013-04-11' AND s.date='2013-04-11'

How do I correct my query to get the buyers name from the buyer table.

        String command =
                "SELECT "
                + "t.sellerID, t.buyerID, t.head, t.descrip, "
                + "ROUND(t.weight/t.head) AS AVG_WGT, "
                + "t.weight, t.price, t.date, "
                + "s.name, "
                + "s.date, "
                + "t.type AS sexcode "
                + "FROM trans AS t "
                + "JOIN seller AS s ON t.sellerid = s.number "
-->           + "AND buyer AS b ON t.buyerID = b.number "
                + "WHERE t.date= '" + reportDate + "' "
                + "AND s.date= '" + reportDate + "' "
                + "ORDER BY s.name ASC, sexcode DESC";
       
        try {
            st = (Statement) con.createStatement();
            rs = st.executeQuery(command);

            if (!rs.isBeforeFirst() ) {
                 JOptionPane.showMessageDialog(null,
                    "No data found!");
            }
            while (rs.next()) {
                String name = rs.getString("name");

                if (rs.wasNull()) {
                    name = "";
                }
                String head = rs.getString("head");
                String desc = rs.getString("descrip");
                String avgwgt = rs.getString("avg_wgt");
                String weight = rs.getString("weight");
                String bidPrice = rs.getString("price");
                String transDate = rs.getString("date");
0
Comment
Question by:sargent240
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 25

Expert Comment

by:chaau
ID: 39640656
You need to use JOIN:

 String command =
                "SELECT "
                + "t.sellerID, t.buyerID, t.head, t.descrip, "
                + "ROUND(t.weight/t.head) AS AVG_WGT, "
                + "t.weight, t.price, t.date, "
                + "s.name, "
                + "s.date, "
                + "t.type AS sexcode "
                + "FROM trans AS t "
                + "INNER JOIN seller AS s ON t.sellerid = s.number "
                + "INNER JOIN buyer AS b ON t.buyerID = b.number "
                + "WHERE t.date= '" + reportDate + "' "
                + "AND s.date= '" + reportDate + "' "
                + "ORDER BY s.name ASC, sexcode DESC";

Open in new window


Please note that if buyer does not always exist for your transaction (unlikely, but just in case), you need to modify INNER JOIN to LEFT JOIN
0
 

Author Comment

by:sargent240
ID: 39640679
Great!  The syntax error went away.  The only other thing is, you will notice  the first line after the

while(rs.next()) {

statement is a

String name = rs.getString("name");

That statement puts the sellers name from the seller table into the name variable.  I am not to sure what statement to use to get the buyers name from the buyers file.  The table description is below and the buyers name is in the name field.  Can you help me with that?  Thanks

mysql> describe buyer;
+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| id          | int(11)      | NO   | PRI | NULL    | auto_increment |
| customer_id | int(11)      | YES  |     | NULL    |                |
| date        | date         | YES  |     | NULL    |                |
| number      | varchar(64)  | YES  |     | NULL    |                |
| name        | varchar(255) | YES  |     |         |                |
| address1    | varchar(255) | YES  |     |         |                |
| address2    | varchar(255) | YES  |     |         |                |
| city        | varchar(255) | YES  |     |         |                |
| state       | varchar(4)   | YES  |     |         |                |
| zip         | varchar(20)  | YES  |     |         |                |
| company     | varchar(255) | YES  |     |         |                |
| comment     | varchar(255) | YES  |     |         |                |
+-------------+--------------+------+-----+---------+----------------+
12 rows in set (0.00 sec)

Open in new window

0
 
LVL 35

Assisted Solution

by:mccarl
mccarl earned 100 total points
ID: 39640685
Well, first you need to actually "select" that field in your query. At the moment, the query isn't even retrieving that field yet. And then when you select it, you can just put an alias on that (just like you already have done for "AVG_WGT" or "sexcode") and then specify that alias in your rs.getString() method call.
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 48

Expert Comment

by:PortletPaul
ID: 39640695
>> I am not to sure what statement to use to get the buyers name from the buyers file.
The buyers name is available to your query via the table alias b

so:

b.name

Just include "b.name" into you select clause (perhaps instead of s.name?).


      by the way, It's the buyers table, not buyers file :)
0
 
LVL 25

Accepted Solution

by:
chaau earned 400 total points
ID: 39640696
Just add buyer name to the list of selected columns, like this:
 String command =
                "SELECT "
                + "t.sellerID, t.buyerID, t.head, t.descrip, "
                + "ROUND(t.weight/t.head) AS AVG_WGT, "
                + "t.weight, t.price, t.date, "
                + "s.name, "
                + "s.date, "
                + "t.type AS sexcode, "
                + "b.name AS buyerName "
                + "FROM trans AS t "
                + "INNER JOIN seller AS s ON t.sellerid = s.number "
                + "INNER JOIN buyer AS b ON t.buyerID = b.number "
                + "WHERE t.date= '" + reportDate + "' "
                + "AND s.date= '" + reportDate + "' "
                + "ORDER BY s.name ASC, sexcode DESC";

Open in new window

And then just use it in the code:
String buyerName = rs.getString("buyerName");

Open in new window

0
 

Author Closing Comment

by:sargent240
ID: 39640717
Thank you very much for the help.  I'm on the road again and know enough to be dangerous.
Cheers
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question