基于另一个值的更新现有表

时间:2016-04-05 20:55:28

标签: mysql join sql-update

我有两张桌子

Book Table 
+--------+------+------+--------+
| EmpID  |BookID| Type | Color  |
+--------+------+------+--------+
|      1 |    A |    0 |Blue    |
|      2 |    B |    0 |Black   |
|      3 |    C |    0 |White   |
|      4 |    D |    0 |Blue    |
+--------+------+------+--------+
     ->Color  Can Be only 'Blue' or 'Black' or 'White'
     ->EmpID and BookId are Unique

Settings Table
+--------+------------+--------------+
| EmpID  | Blue       |  Black/White |
+--------+------------+--------------+
|      1 | 0          |      0       |
|      2 | 0          |      1       |
|      3 | 1          |      0       |
|      4 | 1          |      1       |
+--------+------------+--------------+
    ->For Emp1 Blue and Black are Not Allowed
    ->For Emp2 Only Black/White are Allowed

我在Book Table上添加了一个名为TYPE的新列,并将其默认为'0'。现在我需要根据设置表中的逻辑更新类型的值&颜色列值。

  LOGIC For Updating
 +--------+------------+------------+-------+
| Color  | Blue       | Black/White| TYPE   |
+--------+------------+---------------------+
|  blue  | 0          |      0     |   0    |
|  white | 0          |      1     |   1    |
|  black | 1          |      0     |   0    |
|  blue  | 1          |      1     |   1    |
+--------+------------+------------+--------+
   -> If Color is BLUE and BLUE_SETTING is 1 then TYPE is 1 else 0
   -> If Color is BLACK OR WHITE and BLACK/WHITE Setting is 1 then TYPE is 1 else 0

0 个答案:

没有答案