简体   繁体   English

使用自动增量和外键在表上插入/更新

[英]Insert/Update on table with autoincrement and foreign key

I have a table as such: 我有这样一个表:

id entity_id  first_year  last_year sessions_attended age

1   2020       1996         2008         3            34.7
2   2024       1993         2005         2            45.1
3    ...       ...          ...

id is auto-increment primary key, and entity_id is a foreign key that must be unique for the table. id是自动递增的主键,而entity_id是该表必须唯一的外键。

I have a query that calculates first and last year of attendance, and I want to be able to update this table with fresh data each time it is run, only updating the first and last year columns: 我有一个查询来计算出勤的第一年和去年,并且我希望每次运行时都能用新数据更新该表,而仅更新第一和去年列:

This is my insert/update for "first year": 这是我对“第一年”的插入/更新:

    insert into my_table (entity_id, first_year)
( select contact_id, @sd:= year(start_date) 
from 
( select  contact_id, event_id, start_date from participations
    join events on participations.event_id = events.id where events.event_type_id = 7
    group by contact_id order by event_id ASC) as starter) 
    ON DUPLICATE KEY UPDATE first_year_85 = @sd;

I have one similar that does "last year", identical except for the target column and the order by. 我有一个类似“去年”的项目,除了目标列和排序依据外,其余均相同。

The queries alone return the desired values, but I am having issues with the insert/update queries. 仅查询返回所需的值,但是我在插入/更新查询时遇到问题。 When I run them, I end up with the same values for both fields (the correct first_year value). 运行它们时,两个字段的值都相同(正确的first_year值)。

Does anything stand out as the cause for this? 是什么原因引起的?

Anecdotal Note: This seems to work on MySQL 5.5.54, but when run on my local MariaDB, it just exhibits the above behavior... 轶事说明:这似乎可以在MySQL 5.5.54上运行,但是在我本地的MariaDB上运行时,它仅表现出上述行为...

Update: 更新:

Not my table design to dictate. 不是我的桌子设计要支配的。 This is a CRM that allows custom fields to be defined by end-users, I am populating the data via external queries. 这是一个CRM,允许最终用户定义自定义字段,我通过外部查询填充数据。

The participations table holds all event registrations for all entity_ids, but the start dates are held in a separate events table, hence the join. 参与表包含所有entity_id的所有事件注册,但是开始日期保存在单独的事件表中,因此是联接。

The variable is there because the ON DUPLICATE UPDATE will not accept a reference to the column without it. 存在该变量是因为ON DUPLICATE UPDATE将不接受对该列的引用。

Age is actually slightly more involved: It is age by the start date of the next active event of a certain type. 实际上,年龄要稍微复杂一些:它是某个类型的下一个活动事件的开始日期之前的年龄。

Fields are being "hard" updated as the values in this table are being pulled by in-CRM reports and searches, they need to be present, can't be dynamically calculated. 字段正在“硬”更新,因为此表中的值已被CRM内的报告和搜索提取,它们必须存在且无法动态计算。

Since you have a 'natural' PK ( entity_id ), why have the id ? 由于您具有“自然” PK( entity_id ),因此为什么具有id

age ? age Are you going to have to change that column daily, or at least monthly? 您是否必须每天或至少每月更改该列? Not a good design. 不是一个好的设计。 It would be better to have the constant birth_date in the table, then compute the ages in SELECT . 最好在表中有一个恒定的birth_date,然后在SELECT计算年龄。

"calculates first and last year of attendance" -- This implies you have a table that lists all years of attendance ( yoa )? “计算出勤的第一年和最后一年”-这意味着您有一张表列出了所有出勤年份( yoa )? If so, MAX(yoa) and MIN(yoa) would probably a better way to compute things. 如果是这样,则MAX(yoa)MIN(yoa)可能是一种更好的计算方式。

One rarely needs @variables in queries. 一个很少需要查询中的@variables。

Munch on my comments; 蒙克对我的评论; come back for more thoughts after you provide a new query, SHOW CREATE TABLE , EXPLAIN , and some sample data. 提供新查询SHOW CREATE TABLEEXPLAIN和一些示例数据后,请返回以获取更多想法。

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

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