Solved

Mysql

Posted on 2013-11-11
7
522 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 48

Expert Comment

by:PortletPaul
Comment Utility
>> 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
Comment Utility
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
Comment Utility
Thank you very much for the help.  I'm on the road again and know enough to be dangerous.
Cheers
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Java asynchronous logging 4 31
JDeveloper 12c for 32 bit 4 33
Non-recursive backtracking, using a stack 1 43
query question 13 40
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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 basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now