自动为用户分配记录

时间:2013-01-14 05:59:57

标签: php mysql

我有一个有多个表的MySQL数据库。使用VIEW我根据条件生成结果,例如这是我的查询查询:

select * from 
 (
  (
   (`customer_info` join `deals`) 
   left join 
   `account_info` 
   on
   (
    (`account_info`.`custID` = `customer_info`.`custID`)
   )
  ) 
  left join `phone_details` 
  on
  (
   (`phone_details`.`dealID` = `deals`.`dealID`)
  )
 ) 
 where 
 (
  (`customer_info`.`custID` = `deals`.`custID`) 
  and
  (`deals`.`precredit` <> 'Complete')
 ) 
 group by 
  `deals`.`dealID` 
 having 
 (
  (100 - ((
   (sum(`phone_details`.`ordered`) - sum(`phone_details`.`received`)) 
   / sum(`phone_details`.`ordered`)) * 100)) 
  >= 60
 )

这几乎给了我一份已收到至少60%订单的客户名单:

custID |  customer name  | ordered  | received
1      |  Customer 1     | 5        | 3  
2      |  Customer 2     | 4        | 3
3      |  Customer 3     | 2        | 2
4      |  Customer 4     | 1        | 1

现在,我想做的是从这些结果中,自动为用户分配,假设我有user1,user2和user3。所以我的结果现在看起来像这样:

custID |  customer name  | ordered  | received  | user
1      |  Customer 1     | 5        | 3         | user1
2      |  Customer 2     | 4        | 3         | user2
3      |  Customer 3     | 2        | 2         | user3
4      |  Customer 4     | 1        | 1         | user1

我将如何进行这种情况?

0 个答案:

没有答案