SQL如何在条件下将完整的数组放在临时表中?

发布于 2025-01-27 23:57:40 字数 1325 浏览 3 评论 0原文

好吧,我真的很抱歉,因为我的解释是如此糟糕。谢谢您的所有答案。

我将更好地解释什么应该是输出,我的问题是什么。

因此,首先,我有一系列的tagcodes,例如('code0','code1','code2')。 然后,我有一个包含代码和tagtypeid的表。

我想进入一个临时表,其中所有我在数组中传递的所有代码都带有他们的tagtypeid。因此,一个表格:

代码tagtypeid
903420012408181609019A182456
90342001242408181609019A182135
TESTCODENULLNULL NULL

,所以我的尝试是这样:

SELECT Tags.Code AS tagCode, Tags.TagTypeId, TagTypes.Code AS tagType 
INTO #TempTable
FROM Tags JOIN TagTypes ON Tags.TagTypeId = TagTypes.Id 
WHERE Tags.Code IN ('903420012408181609019A18','90341808151313061101E938', 'TestCodeNull')
SELECT * FROM #TempTable;

但是我没有在标签表中获得任何代码。

我这样做了,它似乎正在按预期工作:

CREATE TABLE #TestTable (tagCode NVARCHAR(25) NOT NULL, TagTypeId INT NULL, tagType NVARCHAR(MAX))
INSERT INTO #TestTable (tagCode) VALUES ('903420012408181609019A18'),('00007E08190D0A34E1F524D0'),('00007E08190D0B25E1F5A98B')
UPDATE #TestTable SET TagTypeId = Tags.TagTypeId, tagType = TagTypes.Code FROM #TestTable
LEFT JOIN Tags ON (#TestTable.tagCode = Tags.Code)
LEFT JOIN TagTypes ON (Tags.TagTypeId = TagTypes.Id)
SELECT * FROM #TestTable;

Well, I am really sorry because my explanation was so poor. Thank you for all the answers.

I will explain better what should be the output and what is my question.

So, first of I have an array of tagCodes like ('code0','code1','code2').
Then I have a table that contains Codes and TagTypeId.

I would like to get into a temporary table all the codes I passed in the array with their TagTypeId. So a table like:

CodeTagTypeId
903420012408181609019A182456
903420012408181609019A182135
TestCodeNullnull

So my attempt was this one:

SELECT Tags.Code AS tagCode, Tags.TagTypeId, TagTypes.Code AS tagType 
INTO #TempTable
FROM Tags JOIN TagTypes ON Tags.TagTypeId = TagTypes.Id 
WHERE Tags.Code IN ('903420012408181609019A18','90341808151313061101E938', 'TestCodeNull')
SELECT * FROM #TempTable;

But I dont get the codes that are not in the Tag table.

I did this an it seems to be working as intended:

CREATE TABLE #TestTable (tagCode NVARCHAR(25) NOT NULL, TagTypeId INT NULL, tagType NVARCHAR(MAX))
INSERT INTO #TestTable (tagCode) VALUES ('903420012408181609019A18'),('00007E08190D0A34E1F524D0'),('00007E08190D0B25E1F5A98B')
UPDATE #TestTable SET TagTypeId = Tags.TagTypeId, tagType = TagTypes.Code FROM #TestTable
LEFT JOIN Tags ON (#TestTable.tagCode = Tags.Code)
LEFT JOIN TagTypes ON (Tags.TagTypeId = TagTypes.Id)
SELECT * FROM #TestTable;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遇见了你 2025-02-03 23:57:40

我认为您的意思是标签中不存在“ testcodenull”,因此您想向null展示“ testcodenull”的null,在这种情况下,加入可能更合适。例如

SELECT S.CODE,Tags.Code AS tagCode, Tags.TagTypeId, 
       TagTypes.Code AS tagType 
INTO #TempTable
FROM (select '903420012408181609019A18' code 
      union all select '90341808151313061101E938'
      union all select 'TestCodeNull') s
left join Tags on tags.code = s.code 
left JOIN TagTypes ON Tags.TagTypeId = TagTypes.Id 
 
SELECT * FROM #TempTable;

I think what you mean that 'TestCodeNull' does not exist in tags so you want to show null for 'TestCodeNull' in which case a join may be more appropriate. for example

SELECT S.CODE,Tags.Code AS tagCode, Tags.TagTypeId, 
       TagTypes.Code AS tagType 
INTO #TempTable
FROM (select '903420012408181609019A18' code 
      union all select '90341808151313061101E938'
      union all select 'TestCodeNull') s
left join Tags on tags.code = s.code 
left JOIN TagTypes ON Tags.TagTypeId = TagTypes.Id 
 
SELECT * FROM #TempTable;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文