2014-11-04 54 views
-1

我有一个触发器,我试图写入另一个表时写入表。MySQL插入到另一个表时触发问题

的问题是,以表我是从被存储在类似这样的几行1个条目读出(不是我的桌子,一个插件编写此):

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

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

408  [email protected]   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 

我使用下面写我的新表:

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 
    ) 

不幸的是它被写入几次,只有最上面的条目是正确的1,我想保持它有所有的值和无NULLS:

892  test5 test5 [email protected]  12345678909  Movable Assets,Movable Assets NULL 1 2014-11-04 15:43:35 

891  test5 test5 [email protected]  12345678909  NULL       NULL 1 2014-11-04 15:43:35 

890  test5 test5 [email protected]  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 

任何援助将不胜感激。

+0

我可以帮助你,如果你可以在[SQL Fiddle](http://sqlfiddle.com/)中创建表的实例并插入语句, – Payam 2014-11-04 14:51:32

回答

2

您发布的问题可能没有问题,但您不确定自己需要什么。如果我正确理解了你的要求,那么这就是你给出的答案。只需包含限制空值的where条件即可。

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

在这里你可以包括不应该是空值的列(例如电子邮件,电话和其他)。如果不起作用,请不要犹豫,问问或发布问题。我们一直在这里帮助你。

相关问题