- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
大家好,有谁知道如何过滤数据或使用类似运算符对 JSON_ARRAYAGG() 中的数据进行排序,
我想在以下查询中添加 Like 运算符。我是 MySQL 新手,如何实现这一点
SELECT s.state_id,s.state,
JSON_ARRAYAGG(JSON_OBJECT('city',c.city, 'city_id',c.city_id) /* i want to add like Operator here*/)
AS 'cities'
FROM `cities` AS c JOIN states AS s
WHERE s.state_id = c.state_id GROUP BY State.
我想使用 like 运算符对城市进行排序
create table `cities` (
`city_id` int (11),
`city` varchar (270),
`state_id` int (11),
`is_active` tinyint (1),
`created_at` datetime ,
`updated_at` datetime
);
insert into `cities` (`city_id`, `city`, `state_id`, `is_active`, `created_at`, `updated_at`) values('1','Port Blair*','1','1','2021-03-31 07:25:28','2021-03-31 07:25:28');
insert into `cities` (`city_id`, `city`, `state_id`, `is_active`, `created_at`, `updated_at`) values('2','Adoni','2','1','2021-03-31 07:25:28','2021-03-31 07:25:28');
insert into `cities` (`city_id`, `city`, `state_id`, `is_active`, `created_at`, `updated_at`) values('3','Amalapuram','2','1','2021-03-31 07:25:28','2021-03-31 07:25:28');
insert into `cities` (`city_id`, `city`, `state_id`, `is_active`, `created_at`, `updated_at`) values('4','Anakapalle','2','1','2021-03-31 07:25:28','2021-03-31 07:25:28');
insert into `cities` (`city_id`, `city`, `state_id`, `is_active`, `created_at`, `updated_at`) values('5','Anantapur','2','1','2021-03-31 07:25:28','2021-03-31 07:25:28');
insert into `cities` (`city_id`, `city`, `state_id`, `is_active`, `created_at`, `updated_at`) values('6','Bapatla','2','1','2021-03-31 07:25:28','2021-03-31 07:25:28');
insert into `cities` (`city_id`, `city`, `state_id`, `is_active`, `created_at`, `updated_at`) values('7','Bheemunipatnam','2','1','2021-03-31 07:25:28','2021-03-31 07:25:28');
状态表:
create table `states` (
`state_id` int (11),
`state` varchar (180),
`is_active` tinyint (1),
`created_at` datetime ,
`updated_at` datetime
);
insert into `states` (`state_id`, `state`, `is_active`, `created_at`, `updated_at`) values('1','Andaman and Nicobar Islands','1','2021-03-31 06:30:04','2021-03-31 06:30:04');
insert into `states` (`state_id`, `state`, `is_active`, `created_at`, `updated_at`) values('2','Andhra Pradesh','1','2021-03-31 06:30:04','2021-03-31 06:30:04');
insert into `states` (`state_id`, `state`, `is_active`, `created_at`, `updated_at`) values('3','Arunachal Pradesh','1','2021-03-31 06:30:04','2021-03-31 06:30:04');
insert into `states` (`state_id`, `state`, `is_active`, `created_at`, `updated_at`) values('4','Assam','1','2021-03-31 06:30:04','2021-03-31 06:30:04');
insert into `states` (`state_id`, `state`, `is_active`, `created_at`, `updated_at`) values('5','Bihar','1','2021-03-31 06:30:04','2021-03-31 06:30:04');
insert into `states` (`state_id`, `state`, `is_active`, `created_at`, `updated_at`) values('6','Chandigarh','1','2021-03-31 06:30:04','2021-03-31 06:30:04');
insert into `states` (`state_id`, `state`, `is_active`, `created_at`, `updated_at`) values('7','Chhattisgarh','1','2021-03-31 06:30:04','2021-03-31 06:30:04');
insert into `states` (`state_id`, `state`, `is_active`, `created_at`, `updated_at`) values('8','Dadra and Nagar Haveli','1','2021-03-31 06:30:04','2021-03-31 06:30:04');
insert into `states` (`state_id`, `state`, `is_active`, `created_at`, `updated_at`) values('9','Delhi','1','2021-03-31 06:30:04','2021-03-31 06:30:04');
insert into `states` (`state_id`, `state`, `is_active`, `created_at`, `updated_at`) values('10','Goa','1','2021-03-31 06:30:04','2021-03-31 06:30:04');
insert into `states` (`state_id`, `state`, `is_active`, `created_at`, `updated_at`) values('11','Gujarat','1','2021-03-31 06:30:04','2021-03-31 06:30:04');
insert into `states` (`state_id`, `state`, `is_active`, `created_at`, `updated_at`) values('12','Haryana','1','2021-03-31 06:30:04','2021-03-31 06:30:04');
insert into `states` (`state_id`, `state`, `is_active`, `created_at`, `updated_at`) values('13','Himachal Pradesh','1','2021-03-31 06:30:04','2021-03-31 06:30:04');
fiddle
最佳答案
https://dev.mysql.com/doc/refman/5.7/en/aggregate-functions.html#function_json-arrayagg说:
JSON_ARRAYAGG(col_or_expr)
Aggregates a result set as a single JSON array whose elements consist of the rows. The order of elements in this array is undefined.
JSON_ARRAYAGG()
聚合之前对元素进行排序的方法.该票证处于“已验证”状态,这意味着它已被接受为功能请求,但尚未实现。
mysql> select * from mytable;
+----+-------------+------+
| id | text | x |
+----+-------------+------+
| 1 | longing | NULL |
| 2 | rusted | NULL |
| 3 | seventeen | NULL |
| 4 | daybreak | NULL |
| 5 | furnace | NULL |
| 6 | nine | NULL |
| 7 | benign | NULL |
| 8 | homecoming | NULL |
| 9 | one | NULL |
| 10 | freight car | NULL |
+----+-------------+------+
在这里,我们看到我的表中的一些单词按字母顺序返回:
mysql> select json_arrayagg(text) from mytable;
+-----------------------------------------------------------------------------------------------------------------+
| json_arrayagg(text) |
+-----------------------------------------------------------------------------------------------------------------+
| ["benign", "daybreak", "freight car", "furnace", "homecoming", "longing", "nine", "one", "rusted", "seventeen"] |
+-----------------------------------------------------------------------------------------------------------------+
原因是查询在
text
上使用了索引。列作为访问顺序。因为我只提取了一列,所以它能够进行索引扫描。
mysql> explain select json_arrayagg(text) from mytable;
+----+-------------+---------+------------+-------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+---------+------------+-------+---------------+------+---------+------+------+----------+-------------+
| 1 | SIMPLE | mytable | NULL | index | NULL | text | 83 | NULL | 10 | 100.00 | Using index |
+----+-------------+---------+------------+-------+---------------+------+---------+------+------+----------+-------------+
而如果我包含另一个非索引列,它会执行表扫描,按主键顺序读取行:
mysql> explain select x, json_arrayagg(text) from mytable;
+----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------+
| 1 | SIMPLE | mytable | NULL | ALL | NULL | NULL | NULL | NULL | 10 | 100.00 | NULL |
+----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------+
结果是数组按主键顺序聚合,而不是按字母顺序:
mysql> select x, json_arrayagg(text) from mytable;
+------+-----------------------------------------------------------------------------------------------------------------+
| x | json_arrayagg(text) |
+------+-----------------------------------------------------------------------------------------------------------------+
| NULL | ["longing", "rusted", "seventeen", "daybreak", "furnace", "nine", "benign", "homecoming", "one", "freight car"] |
+------+-----------------------------------------------------------------------------------------------------------------+
我可以制作另一个索引来强制它使用另一个订单吗?
mysql> update mytable set x = 11 - id;
Query OK, 10 rows affected (0.03 sec)
mysql> select * from mytable order by id;
+----+-------------+------+
| id | text | x |
+----+-------------+------+
| 1 | longing | 10 |
| 2 | rusted | 9 |
| 3 | seventeen | 8 |
| 4 | daybreak | 7 |
| 5 | furnace | 6 |
| 6 | nine | 5 |
| 7 | benign | 4 |
| 8 | homecoming | 3 |
| 9 | one | 2 |
| 10 | freight car | 1 |
+----+-------------+------+
mysql> alter table mytable add key (x, text);
EXPLAIN 显示如果我只引用该索引中的两列,它将更喜欢进行索引扫描:
mysql> explain select x, json_arrayagg(text) from mytable;
+----+-------------+---------+------------+-------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+---------+------------+-------+---------------+------+---------+------+------+----------+-------------+
| 1 | SIMPLE | mytable | NULL | index | NULL | x | 88 | NULL | 10 | 100.00 | Using index |
+----+-------------+---------+------------+-------+---------------+------+---------+------+------+----------+-------------+
因为
x
是此索引中的第一个,它确定行聚合的顺序。
mysql> select x, json_arrayagg(text) from mytable;
+------+-----------------------------------------------------------------------------------------------------------------+
| x | json_arrayagg(text) |
+------+-----------------------------------------------------------------------------------------------------------------+
| 1 | ["freight car", "one", "homecoming", "benign", "nine", "furnace", "daybreak", "seventeen", "rusted", "longing"] |
+------+-----------------------------------------------------------------------------------------------------------------+
如果你有更多的列,或者如果表是一个不是连接顺序中第一个的连接表,那么按照你想要的方式获取数组顺序可能会非常棘手——甚至不可能——同时仍然访问表很好的优化。
关于mysql - 有谁知道是否有办法在 JSON_ARRAYAGG() 中实现任何排序或过滤?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66985669/
使用 MySQL 8.0 json_arrayagg被引入,这使得聚合 json 结果成为可能。 现在我想用它来显示附加到消息的标签。 目前有这三个表(简化) CREATE TABLE IF NOT
大家好,有谁知道如何过滤数据或使用类似运算符对 JSON_ARRAYAGG() 中的数据进行排序, 我想在以下查询中添加 Like 运算符。我是 MySQL 新手,如何实现这一点 SELECT s.s
所以我试图将一个不同的单列查询的结果放入一个 JSON 数组中,以便它可以在我的 Web 服务器上使用。我已经设置了这样的东西: SELECT JSON_OBJECT( 'ArrayKey' VA
最近安装了MySQL 8.0.11 GA,想优化一些 View ,函数,...通常我有这样的东西: CREATE TABLE `PublicHoliday` ( `PublicHoliday_ID
我想对 json_arrayagg() 返回的数组进行排序。我的查询与此类似:- select A, json_arrayagg(json_obj('X',value1, 'Y',value2)) A
使用 Oracle 19c: 我有以下查询,其中子选择(通过 plans1_.ID 链接到主选择)使用 JSON_ARRAYAGG功能。 select ... , /* Other colum
也许我错过了什么。但是为什么没有这个功能呢? 根据文档,它应该是: https://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html 版本
问题是数组颜色中带有引号的反斜杠。我认为这是因为 JSON_ARRAYAGG 但我不知道如何打印正确的 json。 查询: SELECT a.id_product, JSON_ARRAYAGG(c.n
我有疑问: SELECT id, JSON_ARRAYAGG(url) AS urlLinks FROM links WHERE id=832781 GROUP BY id; 此查询的结果在urlLi
使用 JSON_ARRAYAGG 函数时,限制子句似乎不起作用。 有没有更好的方法来实现该功能? SELECT * FROM USER_TABLE ORDER BY RAND() LIMIT 2; -
找不到具有兼容参数的名为“FUNCTION”类型的名为“JSON_ARRAYAGG”的授权例程 在DB2中使用JSON_ARRAYAGG函数时出错: QUERY: SELECT
set countt = PAGE_ * 5 - 5; set @data = (select JSON_ARRAYAGG(JSON_OBJECT('id', room_partici
SQL 示例... SELECT ID AS HID, JSON_ARRAYAGG( JSON_OBJECT('sequence': TRIM(SEQUENCE),
我正在使用 Oracle 19c 和 JSON_ARRAYAGG 函数(使用 JSON_OBJECT)返回 JSON 对象的串联数组字符串。我需要根据 ORDER BY SENT_DATE DESC
我是一名优秀的程序员,十分优秀!