Reputation:
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?
Upvotes: 0
Views: 79
Reputation: 13824
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.
Upvotes: 0
Reputation: 1251
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
Upvotes: 1