可以在 中使用 'NULL' 值deal.tariff_id
来表示它属于tariff_data
表中的任何关税affiliate_id=3
吗?
例如:
mysql> select * from phone;
+----+------------------+
| id | name |
+----+------------------+
| 4 | HTC Sensation XL |
| 26 | iPhone 4s |
| 25 | iPhone 5 |
| 24 | Nokia C3-01 |
+----+------------------+
mysql> select * from tariff;
+----+-----------------+-----------------+--------------+-------------+
| id | name | tariff_duration | monthly_cost | description |
+----+-----------------+-----------------+--------------+-------------+
| 1 | Business Plan 1 | 24 | 5.00 | |
| 2 | Business Plan 2 | 24 | 10.00 | |
| 4 | Business Plan 3 | 24 | 15.52 | |
| 5 | Business Plan 4 | 24 | 18.52 | |
| 8 | Super Plan | 12 | 15.00 | |
+----+-----------------+-----------------+--------------+-------------+
mysql> select * from tariff_data;
+----+-----------+--------------+-------+
| id | tariff_id | affiliate_id | bonus |
+----+-----------+--------------+-------+
| 1 | 1 | 3 | 34.00 |
| 2 | 2 | 3 | 44.00 |
| 5 | 3 | 3 | 10.00 |
| 6 | 4 | 3 | 10.00 |
| 7 | 5 | 3 | 10.00 |
+----+-----------+--------------+-------+
在一张deal
表上,您可以看到affiliate_id=3
并tariff_id=NULL
属于tariff_data
表中列出的任何关税。我这样做是为了减少deal
. 如果我不包含 NULL ,则意味着我必须包含许多tariff_id
相同的内容phone_id
,反之亦然。
mysql> select * from deal;
+----+----------+-----------+--------------+--------+
| id | phone_id | tariff_id | affiliate_id | active |
+----+----------+-----------+--------------+--------+
| 1 | 4 | NULL | 3 | 1 |
| 3 | 24 | NULL | 3 | 1 |
| 9 | 24 | 8 | 4 | 1 |
| 10 | 25 | 8 | 4 | 1 |
| 11 | 26 | 8 | 4 | 1 |
+----+----------+-----------+--------------+--------+
更新,例如,如果我没有使用 NULL 值:
mysql> select * from deal;
+----+----------+-----------+--------------+--------+
| id | phone_id | tariff_id | affiliate_id | active |
+----+----------+-----------+--------------+--------+
| 1 | 4 | 1 | 3 | 1 |
| 2 | 4 | 2 | 3 | 1 |
| 3 | 24 | 1 | 3 | 1 |
| 4 | 24 | 2 | 3 | 1 |
| 5 | 24 | 3 | 3 | 1 |
| 6 | 24 | 4 | 3 | 1 |
| 7 | 24 | 5 | 3 | 1 |
| 9 | 24 | 8 | 4 | 1 |
| 10 | 25 | 8 | 4 | 1 |
| 11 | 26 | 8 | 4 | 1 |
+----+----------+-----------+--------------+--------+