[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Mysql

Posted on 2013-11-11
7
Medium Priority
?
531 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

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 article, I’ll talk about multi-threaded slave statistics printed in MySQL error log file.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

649 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