user10078987
user10078987

Reputation:

MySql and java {getting related tables columns}

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

Answers (2)

Yash
Yash

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

Ramana V V K
Ramana V V K

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

Related Questions