?
Solved

Mysql

Posted on 2013-11-11
7
Medium Priority
?
530 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 36

Assisted Solution

by:mccarl
mccarl earned 400 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 49

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 1600 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

How Blockchain Is Impacting Every Industry

Blockchain expert Alex Tapscott talks to Acronis VP Frank Jablonski about this revolutionary technology and how it's making inroads into other industries and facets of everyday life.

Question has a verified solution.

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

Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

764 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