繁体   English   中英

联接中另一个表的不同列值

[英]DISTINCT COLUMN VALUES FROM ANOTHER TABLE IN JOIN

select t1.key,t2.design,t1.price,t1.gender,t1.store
from table1 t1,table2 t2
where t1.key=t2.key;

这是我的查询。 在此查询中,KEY列是不同的。 我需要具有不同DESIGN值的结果。 帮帮我

from table1 t1 join table2 t2 on t1.key = t2.key
where design like "specific design"

您的要求不准确。 有两种可能:

1)您谈论的是重复记录,即对于具有相同设计的两条记录,键,价格,性别和商店的列保证相等。 有两种解决方法:

select DISTINCT t1.key,t2.design,t1.price,t1.gender,t1.store
from table1 t1,table2 t2
where t1.key=t2.key;

要么

select t1.key,t2.design,t1.price,t1.gender,t1.store
from table1 t1, (select distinct key, design from table2) t2
where t1.key=t2.key;

2)您正在谈论重复的设计和相关的含糊信息,即对于同一设计的两条记录,您可能会得到不同的价格等。然后,您必须考虑要为每个设计获取哪些信息。 最高价? 价格总和? ...

select t1.key,t2.design,sum(t1.price),max(t1.gender),max(t1.store)
from table1 t1,table2 t2
where t1.key=t2.key
group by t1.key,t2.design;

这为您提供了每个键和设计的记录。 如果只希望按设计记录,则只能按设计分组并决定要与哪个键一起显示。

最后一条建议:使用显式联接语法。 它更易于阅读且不易出错。

select t1.key, t2.design, t1.price, t1.gender, t1.store
from table1 t1
inner join table2 t2 on t1.key = t2.key;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM