这个是建2个表的语句,一个是人员表,一个职位表,求出职位空缺数,写到职位表的SPACE里INSERT INTO job_message
   (`position_id`, `position_name`, `total`, `level`, `space`, `department`, `department_id`, `detail`, `found_date`, `del`)
VALUES
   (9, 'a', 20, '', 20, 'x', 1, '', '2011-08-03 09:42:26', 1);INSERT INTO job_message
   (`position_id`, `position_name`, `total`, `level`, `space`, `department`, `department_id`, `detail`, `found_date`, `del`)
VALUES
   (10, 'xx', 12, '', 12, 'y', 2, '', '2011-08-03 09:44:26', 1);INSERT INTO job_message
   (`position_id`, `position_name`, `total`, `level`, `space`, `department`, `department_id`, `detail`, `found_date`, `del`)
VALUES
   (11, '4部1分', 2, '', 2, '4部', 4, '', '2011-08-03 11:56:34', 1);
第二个表INSERT INTO employee_info
   (`user_id`, `password`, `name`, `sex`, `department`, `position_id`, `position`, `office_number`, `mail`, `birthday`, `ismarry`, `telephone`, `postcode`, `address`, `id_card`, `found_date`, `del`, `budget`, `submit`, `sys`, `formInfo`)
VALUES
   (58, '', '', NULL, 'y', 10, 'xx', '', '', '', NULL, '', '', '', '', '2011-08-03 09:44:38', 1, NULL, NULL, NULL, NULL);INSERT INTO employee_info
   (`user_id`, `password`, `name`, `sex`, `department`, `position_id`, `position`, `office_number`, `mail`, `birthday`, `ismarry`, `telephone`, `postcode`, `address`, `id_card`, `found_date`, `del`, `budget`, `submit`, `sys`, `formInfo`)
VALUES
   (59, '', '', NULL, 'y', 10, 'xx', '', '', '', NULL, '', '', '', '', '2011-08-03 09:45:01', 1, NULL, NULL, NULL, NULL);INSERT INTO employee_info
   (`user_id`, `password`, `name`, `sex`, `department`, `position_id`, `position`, `office_number`, `mail`, `birthday`, `ismarry`, `telephone`, `postcode`, `address`, `id_card`, `found_date`, `del`, `budget`, `submit`, `sys`, `formInfo`)
VALUES
   (60, '', '', NULL, 'y', 10, 'xx', '', '', '', NULL, '', '', '', '', '2011-08-03 09:45:24', 1, NULL, NULL, NULL, NULL);INSERT INTO employee_info
   (`user_id`, `password`, `name`, `sex`, `department`, `position_id`, `position`, `office_number`, `mail`, `birthday`, `ismarry`, `telephone`, `postcode`, `address`, `id_card`, `found_date`, `del`, `budget`, `submit`, `sys`, `formInfo`)
VALUES
   (65, '', '', NULL, 'x', 9, 'a', '', '', '', NULL, '', '', '', '', '2011-08-03 10:48:55', 1, NULL, NULL, NULL, NULL);
这个是我写的执行语句,没有效果。。
update job_message,employee_info, (
select  count(employee_info.position_id) as employeed ,position_name from employee_info
  right join job_message on  job_message.position_id =  employee_info.position_id  where job_message.del=1 
and job_message.del=1
group by employee_info.position_id ) as p set job_message.space=job_message.total-case when p.employeed is null then 0 else p.employeed end  where job_message.position_id =  employee_info.position_id and job_message.del=1;

解决方案 »

  1.   

    有空值,group by 排序有问题,我加了个ORDER BY 之后结果有变化了,但是第二条数据和第一条变化一样。update job_message,employee_info, (
    select  count(employee_info.position_id) as employeed ,position_name from employee_info
      right join job_message on  job_message.position_id =  employee_info.position_id  where job_message.del=1 
    and job_message.del=1
    group by employee_info.position_id order by  isnull(employee_info.position_id) ,employee_info.position_id  ) as p set job_message.space=job_message.total-(case when p.employeed is null then 0 else p.employeed end ) where job_message.position_id =  employee_info.position_id and job_message.del=1;
      

  2.   

     (不要高估你的汉语表达能力或者我的汉语理解能力)
       建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
       参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
       
       1. 你的 create table xxx .. 语句
       2. 你的 insert into xxx ... 语句
       3. 结果是什么样,(并给以简单的算法描述)
       4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
       
       这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。