0

I have created 2 tables in MySQL [items, orderList], the foreign key id in orderlist references the primary key id in items. Now I want to take all columns{id, name, price (in Items), and quantity (in orderList)} from 2 tables in Java, how can I show id once because when I query data it shows id from both tables?

2
  • Show what you have tried ? Commented Jul 14, 2018 at 5:44
  • what you tried exactly? you can do that with join queries Commented Jul 14, 2018 at 5:46

2 Answers 2

1

You can do with join queries, try the below query and select the fields whatever you want from two tables

SELECT items.id, items.name, items.price, orderList.quantity
    FROM items INNER JOIN orderList ON items.id = orderList.id
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks sir, it works. I have another question, how can set a value for a column; my mean is how to set a total Price coulmn to gets its value from price and quanitty [price*quantity}
is total price column existing in the table? then only you can use the update query to set the value
0

In order to fetch the data only once, you need to mention where it should come from. You can try the following:

SELECT I.ID, I.NAME, I.PRICE, O.QUANTITY FROM ORDERLIST O, ITEMS I WHERE I.ID = O.ID

Here we have given aliases to both the tables and we have mentioned that the ID column will be picked from the ITEMS table.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.