简体   繁体   中英

MySQL Trigger issue when inserting into another table

I have a trigger which I am trying to get to write to a table when another table is written to.

The issue is that to table I'm reading from is storing 1 entry over several rows like this (not my table, a plugin writes this):

406     test5               74      63      2014-11-04 13:43:35

407     test5               75      63      2014-11-04 13:43:35

408     test5@w.com         76      63      2014-11-04 13:43:35

409     12345678909         78      63      2014-11-04 13:43:35

410     Movable Assets      79      63      2014-11-04 13:43:35

I am using the following to write to my new table:

INSERT INTO `new_memberlist`

(FirstName, LastName, Email, Phone, InterestedIn, Province, Status, DateRegistered)

(
select 
MAX(case when field_id = 74 and entry_id = (SELECT MAX(entry_id) FROM ch_arf_entry_values) then entry_value end) as FirstName, 
MAX(case when field_id = 75 and entry_id = (SELECT MAX(entry_id) FROM ch_arf_entry_values) then entry_value end) as LastName, 
MAX(case when field_id = 76 and entry_id = (SELECT MAX(entry_id) FROM ch_arf_entry_values) then entry_value end) as Email, 
MAX(case when field_id = 78 and entry_id = (SELECT MAX(entry_id) FROM ch_arf_entry_values) then entry_value end) as Phone, 

MAX(case when field_id = 79 and entry_id = (SELECT MAX(entry_id) FROM ch_arf_entry_values) then (
            select distinct concat( 

SUBSTRING_INDEX(SUBSTRING_INDEX( SUBSTRING_INDEX(entry_value,'"',2),'"',2),'"',-1),
',',

SUBSTRING_INDEX(SUBSTRING_INDEX( SUBSTRING_INDEX(entry_value,'"',4),'"',4),'"',-1)
                             )
                         )
            end) as InterestedIn,


MAX(case when field_id = 81 and entry_id = (SELECT MAX(entry_id) FROM ch_arf_entry_values) then (

            select distinct concat( 

SUBSTRING_INDEX(SUBSTRING_INDEX( SUBSTRING_INDEX(entry_value,'"',2),'"',2),'"',-1),
',',
SUBSTRING_INDEX(SUBSTRING_INDEX( SUBSTRING_INDEX(entry_value,'"',4),'"',4),'"',-1),
',',               
SUBSTRING_INDEX(SUBSTRING_INDEX( SUBSTRING_INDEX(entry_value,'"',6),'"',6),'"',-1),
',',
SUBSTRING_INDEX(SUBSTRING_INDEX( SUBSTRING_INDEX(entry_value,'"',8),'"',8),'"',-1),
',',
SUBSTRING_INDEX(SUBSTRING_INDEX( SUBSTRING_INDEX(entry_value,'"',10),'"',10),'"',-1),
',',
SUBSTRING_INDEX(SUBSTRING_INDEX( SUBSTRING_INDEX(entry_value,'"',12),'"',12),'"',-1),
',',
SUBSTRING_INDEX(SUBSTRING_INDEX( SUBSTRING_INDEX(entry_value,'"',14),'"',14),'"',-1),
',',
SUBSTRING_INDEX(SUBSTRING_INDEX( SUBSTRING_INDEX(entry_value,'"',16),'"',16),'"',-1),
',',
SUBSTRING_INDEX(SUBSTRING_INDEX( SUBSTRING_INDEX(entry_value,'"',18),'"',18),'"',-1)                
                             )
                         )
            end) as Province,
'1' as Status,  
NOW() as DateRegistered
from ch_arf_entry_values 
WHERE entry_id = (SELECT MAX(entry_id) FROM ch_arf_entry_values)
GROUP BY entry_id
LIMIT 1
    )

Unfortunately it is being written several times and only the top entry is the correct 1 which I would like to keep which has all the values and no NULLS:

892     test5   test5   test5@w.com     12345678909     Movable Assets,Movable Assets   NULL    1   2014-11-04 15:43:35

891     test5   test5   test5@w.com     12345678909     NULL                            NULL    1   2014-11-04 15:43:35

890     test5   test5   test5@w.com     NULL            NULL                            NULL    1   2014-11-04 15:43:35

889     test5   test5   NULL            NULL            NULL                            NULL    1   2014-11-04 15:43:35

888     test5   NULL    NULL            NULL            NULL                            NULL    1   2014-11-04 15:43:35

887     NULL    NULL    NULL            NULL            NULL                            NULL    1   2014-11-04 15:43:35

Any assistance would be greatly appreciated.

the question you posted might be ok, but you are not sure what you exactly required . If i understood your requirement correctly , then this is the answer given below for you. Just include the where condition which restricts the null values.

WHERE entry_id = (SELECT MAX(entry_id) FROM ch_arf_entry_values) AND firstname!=NULL and lastname!=NULL 

In this you can include the columns ( eg email,phone and others) which shouldn't be the null values. If it not works don't hesitate to ask or post a question . We are always here to help you.

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