插入内部连接到表中

发布于 2025-02-13 08:37:42 字数 980 浏览 0 评论 0原文

我一直在尝试执行带有另一个表的内部连接的表中插入。我试图使用以下的内部加入,但这无效。我不太确定哪个更合适,无论使用内部连接还是左JOIN

INSERT INTO ticketChangeSet (Comments, createdBy, createdDateTime)
VALUES ('Test', 'system', CURRENT_TIMESTAMP)
INNER JOIN tickets ON ticketChangeSet.ticket_id = tickets.id
WHERE tickets.id BETWEEN '3' AND '5'

样本数据:

门票表

id  comment  createdDateTime       closeDateTime          createdBy
2   NULL     2022-07-05 15:36:20   2022-07-05 16:21:03    system
3   NULL     2022-07-05 15:36:20   2022-07-05 16:21:03    system
4   NULL     2022-07-05 15:36:20   2022-07-05 16:21:03    system
5   NULL     2022-07-05 15:36:20   2022-07-05 16:21:03    system

Ticketchangeset表

id  comments               createdBy   createdDateTime        ticket_id
1   Ticket not resolved    system      2022-07-05 15:59:01    2

,我想插入此值('ticke not not vasted','system','2022-07-05 15:59:01')从售票表中的ticket_id ticket_id ticket_id表中

I've been trying to execute insert into a table with an inner join with another table. I tried to use inner join as below but it didn't works. I'm not very sure which is more suitable whether to use INNER JOIN or LEFT JOIN

INSERT INTO ticketChangeSet (Comments, createdBy, createdDateTime)
VALUES ('Test', 'system', CURRENT_TIMESTAMP)
INNER JOIN tickets ON ticketChangeSet.ticket_id = tickets.id
WHERE tickets.id BETWEEN '3' AND '5'

Sample data:

tickets table

id  comment  createdDateTime       closeDateTime          createdBy
2   NULL     2022-07-05 15:36:20   2022-07-05 16:21:03    system
3   NULL     2022-07-05 15:36:20   2022-07-05 16:21:03    system
4   NULL     2022-07-05 15:36:20   2022-07-05 16:21:03    system
5   NULL     2022-07-05 15:36:20   2022-07-05 16:21:03    system

ticketChangeSet table

id  comments               createdBy   createdDateTime        ticket_id
1   Ticket not resolved    system      2022-07-05 15:59:01    2

Basically, I want to insert this value ('Ticket not resolved', 'system', '2022-07-05 15:59:01') into the ticketChangeSet table for ticket_id 3 to 5 from ticket table

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

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

发布评论

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

评论(1

哭泣的笑容 2025-02-20 08:37:42

只需直接从Dillissue(或从票证上 - 不确定)选择行,然后将您的常数作为列值提供。

insert dbo.differIssue (Comments, createdby, dateTime) -- why the strange casing?
select 'Test', 'system', CURRENT_TIMESTAMP 
from dbo.differIssue where Tickets_id between 89 and 100 -- why underscores
;

请注意终结者的语句终结器和模式名称的使用 - 这两个最佳实践。我还假设ID列是数字,并删除了围绕这些过滤器值的字符串定界器。我省略了加入,因为它似乎并不需要。据推测,差异和门票之间的关系是1:1,因此内部联接无济于事。但是,也许您需要在ID值范围内包含来自票证的行,但在不同的ID值中可能不存在?因此,请尝试

insert dbo.differIssue (Comments, createdby, dateTime) 
select 'Test', 'system', CURRENT_TIMESTAMP 
from dbo.Tickets where id between 89 and 100 
;

,但是这一切似乎很可疑。我认为逻辑中至少缺少一个关键列 - 也许不止一个。

更新。现在,您更改了表名,添加了更多列,然后更改了过滤器。您仍然将字符串常数用于数字列 - 一个坏习惯。

insert dbo.ticketChangeSet (...) 
select ... 
from dbo.Tickets as TKT
where not exists (select * from dbo.ticketChangeSet as CHG where CHG.ticket_id = TKT.id)
;

我把它留给你填写缺失的碎屑。

Just select the rows directly from differIssue (or maybe from Tickets - not certain) and supply your constants as the column values.

insert dbo.differIssue (Comments, createdby, dateTime) -- why the strange casing?
select 'Test', 'system', CURRENT_TIMESTAMP 
from dbo.differIssue where Tickets_id between 89 and 100 -- why underscores
;

Notice the statement terminator and the use of schema name - both best practices. I also assumed that the ID column is numeric and removed the string delimiters around those filter values. I left out the join because it did not seem required. Presumably the relationship between differIssue and Tickets is 1:1 so an inner join does nothing useful. But perhaps you need to include rows from Tickets for that range of ID values but which might not exist in differIssue? So try

insert dbo.differIssue (Comments, createdby, dateTime) 
select 'Test', 'system', CURRENT_TIMESTAMP 
from dbo.Tickets where id between 89 and 100 
;

But this all seems highly suspicious. I think there is at least one key column missing from the logic - and perhaps more than one.

Update. Now you've changed the table names, added more columns, and changed the filter. You still use string constants for a numeric column - a bad habit.

insert dbo.ticketChangeSet (...) 
select ... 
from dbo.Tickets as TKT
where not exists (select * from dbo.ticketChangeSet as CHG where CHG.ticket_id = TKT.id)
;

I leave it to you to fill in the missing bits.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文