0

我有一个有多个表的 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

我将如何去做这个场景?

4

0 回答 0