Solved

Mysql

Posted on 2013-11-11
7
524 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
7 Comments
 
LVL 24

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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
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 24

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

823 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