Click here to Skip to main content
15,896,437 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have these tables:
table1: id,name,item_id
item: id,name
itembarcode: id,item_id,barcode

in itembarcode table every item_id has many barcode



i make the following sql:

select a.*, b.name as itemname,c.barcode from table1 a,item b,itembarcode c where a.id<>0 and b.id=a.item_id and c.item_id=a.item_id

in this statement i get every record in table1 but repeated for every barcode in itembarcode for every item
i want to select in only one of barcodes for every item (any barcode of it) "no repeat"

what can i do in this statement?
Posted

1 solution

Try this:
SQL
select distinct a.*, b.name as itemname,
(select top 1 c1.barcode from itembarcode c1 where c1.item_id=c.item_id)
from
table1 a,item b,itembarcode c where
b.id=a.item_id and c.item_id=a.item_id

Suggest you learn to use JOIN [^] in future.
 
Share this answer
 
v2
Comments
M. Daban 23-Jul-14 4:38am    
Yes, its work thankyou
Peter Leow 23-Jul-14 6:20am    
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900