CREATE TABLE `AAAA` (
  `id` int(10) unsigned NOT NULL,
  `level` tinyint(3) unsigned NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM;

INSERT INTO `AAAA` VALUES (2519583,1);

CREATE TABLE `BBBB` (
  `club_id` int(11) NOT NULL DEFAULT '0',
  `profile_id` int(11) NOT NULL DEFAULT '0',
  `member_level_id` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`profile_id`,`club_id`)
) ENGINE=MyISAM;

INSERT INTO `BBBB` VALUES (2,2519583,12);

CREATE TABLE `CCCC` (
  `member_level_id` int(11) unsigned NOT NULL DEFAULT '0',
  `map_level` int(11) unsigned NOT NULL DEFAULT '0',
  `map_status` int(11) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`member_level_id`)
) ENGINE=MyISAM;

INSERT INTO `CCCC` VALUES (12,12,1);

DROP VIEW IF EXISTS `DDDD`;
CREATE ALGORITHM=UNDEFINED 
    DEFINER=`php`@`10.0.0.120` SQL SECURITY INVOKER 
    VIEW `DDDD` AS select `tc`.`club_id` AS `club_id`,`tc`.`profile_id` AS `profile_id`,`map`.`map_level` AS `member_level_id`,`map`.`map_status` AS `member_status`
	from (`BBBB` `tc` join `CCCC` `map` on(((`tc`.`member_level_id` = `map`.`member_level_id`) and (`tc`.`club_id` = 2))));


select level, count(*) as cnt from AAAA group by level;
UPDATE AAAA c LEFT JOIN DDDD t ON (c.id = t.profile_id AND t.club_id = 2) SET c.level = IF (t.member_status IS NULL, 1, IF (t.member_status = 1, 2,3));
select level, count(*) as cnt from AAAA group by level;
