一尘不染

SQL Server-与DISTINCT的内部联接

sql

我很难做到以下几点:

select a.FirstName, a.LastName, v.District
from AddTbl a order by Firstname
inner join (select distinct LastName from
            ValTbl v  where a.LastName = v.LastName)

我想加入ValTbl,但仅适用于不同的值。


阅读 118

收藏
2021-03-10

共1个答案

一尘不染

试试这个:

select distinct a.FirstName, a.LastName, v.District
from AddTbl a 
  inner join ValTbl v
  on a.LastName = v.LastName
order by a.FirstName;

或这样做(它的作用相同,但语法不同):

select distinct a.FirstName, a.LastName, v.District
from AddTbl a, ValTbl v
where a.LastName = v.LastName
order by a.FirstName;
2021-03-10