出于无法控制的原因,我需要在MYSQL中解析键的所有实例的json列,然后获取最大的实例。假设列名称为json_data,而json看起来像这样:
{
list_of_stuff: {
{
value_i_want: 1
},
{
value_i_want: 2
},
{
value_i_want: 4
},
{
value_i_want: 3
}
}
}
在哪里我需要获取value_i_want标签的最大值,在这种情况下为4。
现在我有以下代码:
SELECT JSON_EXTRACT(json_data->"$.list_of_stuff", "$**.value_i_want") from table_name;
但是它返回的是JSON数组,当我尝试获取这些数组的最大值时,它只是返回具有最长json数组的行,而不是每个数组的最大值。
感谢您的帮助!
编辑 我的MYSQL版本是5.7.25
答案 0 :(得分:0)
MYSQL 8.0版。
DB fiddle 创建表Test(id整数,名称json);
insert into Test(id, name) values(1, '{
"list_of_stuff": [
{
"value_i_want": 1
},
{
"value_i_want": 2
},
{
"value_i_want": 4
},
{
"value_i_want": 3
}
]
}');
SELECT @json_var := cast(JSON_EXTRACT(name->"$.list_of_stuff", "$[*]") as char(200)) from Test;
SELECT max(tt.ac)
FROM
JSON_TABLE(
@json_var,
"$[*]"
COLUMNS(
rowid FOR ORDINALITY,
ac int PATH "$.value_i_want"
)
) AS tt;
答案 1 :(得分:0)
根据json验证程序,您的json无效,因此我正在考虑以下有效的json:
private void Button2_Click(object sender, EventArgs e)
{
cmd.CommandType = CommandType.Text;
cmd.Connection = conn;
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
cmd.CommandText = "INSERT INTO tabl(Name_Arabic, CIVILIDD , gender, NATIONALITY, TEL8, TEL7, TEL6, TEL5, TEL4, TEL3, TEL2, TEL1, Work_Adderss, Home_Address, Home_Address2, Name_eng, username, note, Governorate, City, Block, Street, Avenue, House, Floor, flat, address_note, status, confirmation,Irregularities,Irregularities_date)values" +
"(N'" + dataGridView1.Rows[i].Cells[0].Value + "',N'" + dataGridView1.Rows[i].Cells[1].Value + "',N'" + dataGridView1.Rows[i].Cells[2].Value + "',N'" + dataGridView1.Rows[i].Cells[3].Value + "',N'" + dataGridView1.Rows[i].Cells[4].Value + "',N'" + dataGridView1.Rows[i].Cells[5].Value + "',N'" + dataGridView1.Rows[i].Cells[6].Value + "',N'" + dataGridView1.Rows[i].Cells[7].Value + "',N'" + dataGridView1.Rows[i].Cells[8].Value + "',N'" + dataGridView1.Rows[i].Cells[9].Value + "',N'" + dataGridView1.Rows[i].Cells[10].Value + "',N'" + dataGridView1.Rows[i].Cells[11].Value + "',N'" + dataGridView1.Rows[i].Cells[12].Value + "',N'" + dataGridView1.Rows[i].Cells[13].Value + "',N'" + dataGridView1.Rows[i].Cells[14].Value + "',N'" + dataGridView1.Rows[i].Cells[15].Value + "',N'" + dataGridView1.Rows[i].Cells[16].Value + "',N'" + dataGridView1.Rows[i].Cells[17].Value + "',N'" + dataGridView1.Rows[i].Cells[18].Value + "',N'" + dataGridView1.Rows[i].Cells[19].Value + "',N'" + dataGridView1.Rows[i].Cells[20].Value + "',N'" + dataGridView1.Rows[i].Cells[21].Value + "',N'" + dataGridView1.Rows[i].Cells[22].Value + "',N'" + dataGridView1.Rows[i].Cells[23].Value + "',N'" + dataGridView1.Rows[i].Cells[24].Value + "',N'" + dataGridView1.Rows[i].Cells[25].Value + "',N'" + dataGridView1.Rows[i].Cells[26].Value + "',N'" + dataGridView1.Rows[i].Cells[27].Value + "',N'" + dataGridView1.Rows[i].Cells[28].Value + "',N'" + dataGridView1.Rows[i].Cells[29].Value + "',N'" + dataGridView1.Rows[i].Cells[30].Value + "')";
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
MessageBox.Show("saved");
}
这可以与 mysql 5.7 一起使用:
创建mysql函数以返回最大值,然后可以将此函数与任何选择查询一起使用。 see example
{
"list_of_stuff": [
{
"value_i_want": 1
},
{
"value_i_want": 2
},
{
"value_i_want": 4
},
{
"value_i_want": 3
}
]
}