我有表格频道:
+----+----------------+---------+
| id | sort | bouquet |
+----+----------------+---------+
| 1 | ["2","3","73"] | ["1"] |
| 12 | ["3","73"] | ["28"] |
+----+----------------+---------+
并且需要在特定boquet的排序字段中获取计数项目...例如:
花束1有3种物品 花束12有2种物品
我尝试在mysql中使用此查询,但我不知道如何计算排序字段中的项目数:
SELECT COUNT(sort) AS total_channels FROM channels WHERE JSON_SEARCH(bouquet, 'one', "1") IS NOT NULL;
我总是得到:
+----------------+
| total_channels |
+----------------+
| 1 |
+----------------+
哪个不对。
答案 0 :(得分:4)
使用上面的帮助我来到这里:
SELECT JSON_LENGTH(sort) FROM channels WHERE bouquet='["1"]';
我得到正确的数字:
mysql> SELECT JSON_LENGTH(sort) FROM channels WHERE bouquet='["28"]';
+-------------------+
| JSON_LENGTH(sort) |
+-------------------+
| 2 |
+-------------------+
1 row in set (0.00 sec)