从3个表中选择记录而不重复

问题描述:

我有这些表:

table1:id,name,item_id

item:id,name

itembarcode:id,item_id,条形码



在itembarcode表中每个item_id有很多条形码







i生成以下sql:



选择一个。*,b.name作为itemname,c.barcode来自table1 a,item b,itembarcode c其中a.id<> 0和b.id = a.item_id和c.item_id = a.item_id



在此声明中我得到table1中的每条记录但重复每个项目的itembarcode中的每个条形码

i只想在每个项目中选择一个条形码(任何条形码)不重复



我能在这句话中做些什么?

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?

试试这个:

Try this:
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



建议您学会使用 JOIN [


Suggest you learn to use JOIN [^] in future.