是否可以在 deal.tariff_id
中使用“NULL”值来表示它属于 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,这意味着我必须包含许多具有相同 phone_id
的 tariff_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 |
+----+----------+-----------+--------------+--------+
最佳答案
您可以通过多种方式获得所需的结果,但使用比 NULL 更合乎逻辑的方式。
正如 Oded 所指出的,NULL 意味着表示缺失的未知值
- 可以有一列来表明所有权
- 规范化
- 使用现有列“tariff_id”作为具有非空值的所有权指标
关于mysql - 使用 Null 字段来表示某些东西?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12763322/