为什么此更新/选择子查询错误?

时间:2012-07-17 08:53:43

标签: mysql sql select subquery

好的,所以我运行此查询来获取计数,这是正确的:

mysql> select count(medias.fileRef) from medias where fileRef=20193621;
+-----------------------+
| count(medias.fileRef) |
+-----------------------+
| 135869 |
+-----------------------+

然后我运行它将该计数放入另一个表中:

mysql> update files set refCount=(select count(medias.fileRef) 
       from medias where fileRef=20193621) where id=20193621;

Query OK, 1 row affected, 1 warning (0.05 sec)
Rows matched: 1 Changed: 1 Warnings: 1

(请注意,medias.fileRef是一个包含files.id的INT),files.refCount应该包含指向该files.id的所有媒体行的总数。:

然后我去检查它,错了。

mysql> select refcount from files where id=20193621;
+----------+
| refcount |
+----------+
| 127 |
+----------+

这怎么可能?我在这里做错了什么?

1 个答案:

答案 0 :(得分:4)

这是因为您的files.refcount列数据类型为TINYINT。将其更改为INT或任何其他合理的numeric type