简体   繁体   中英

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.

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).

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...

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.

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.

The variable is there because the ON DUPLICATE UPDATE will not accept a reference to the column without it.

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.

Since you have a 'natural' PK ( entity_id ), why have the id ?

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 .

"calculates first and last year of attendance" -- This implies you have a table that lists all years of attendance ( yoa )? If so, MAX(yoa) and MIN(yoa) would probably a better way to compute things.

One rarely needs @variables in queries.

Munch on my comments; come back for more thoughts after you provide a new query, SHOW CREATE TABLE , EXPLAIN , and some sample data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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