从3个表中选择记录而不重复 [英] select records from 3 tables without repeat

查看:69
本文介绍了从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.


这篇关于从3个表中选择记录而不重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆