繁体   English   中英

INNER JOIN查看详细信息另一个表SQL

[英]INNER JOIN to view detail another table SQL

好吧,我有下表(表1):

------------------------------------
GroupID      oDate          oDesc
------------------------------------
1            2016-05-01     A
2            2016-05-20     B
3            2017-03-01     C
4            2017-03-28     D

然后我有下表(表2):

------------------------------------
AutoID      GroupID        oItem
------------------------------------
1           1              abc
2           1              def
3           2              ghi
4           2              jkl
5           3              mno
6           4              pql

我想知道oItem 2中所有具有同一年链接到表1的oItem 结果应该是这样的:

---------------------------
oYear       oItem
---------------------------
2016        abc
2016        def
2016        ghi
2016        jkl
2017        mno
2017        pql

有什么想法怎么做? 谢谢。

使用Year内置函数从日期列中提取年份。 尝试这个

select Year(Odate) as Oyear,B.oItem
from table1 A inner join table2 B
on A.GroupID = B.GroupID 

您可以只使用inner join来获得所需的结果。

select datepart(yyyy, t1.odate) as oyear, t2.oitem
from table1 t1 
inner join table2 t2 on t1.groupid = t2.groupid

暂无
暂无

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

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