modify sql_在SQL Server中使用JSON_MODIFY()修改JSON数据

2023-05-16

modify sql

This article explores JSON_MODIFY() function to modify JSON Data in the SQL Server.

本文探讨了JSON_MODIFY()函数来修改SQL Server中的JSON数据。

介绍 (Introduction)

Java Script Object Notation is a popular language in major NoSQL databases and applications for mobile development. SQL Server 2016 introduced native support for JSON. Suppose you define a variable in SQL Server and it holds JSON key-value pairs.

Java脚本对象表示法是主要的NoSQL数据库和用于移动开发的应用程序中的流行语言。 SQL Server 2016引入了对JSON的本机支持。 假设您在SQL Server中定义了一个变量,并且该变量包含JSON键值对。

We use JSON_MODIFY() function to update the JSON string. It can update the following items:

我们使用JSON_MODIFY()函数更新JSON字符串。 它可以更新以下项目:

  • Update existing property value

    更新现有属性值
  • Add a new element in an existing array

    在现有数组中添加新元素
  • Delete a property from JSON string

    从JSON字符串中删除属性
  • Delete a property

    删除属性

JSON_MODIFY()函数的语法 (Syntax of JSON_MODIFY() function)

JSON_MODIFY (expression, path, newValue)

JSON_MODIFY(表达式,路径,newValue)

  • Expression: It is the JSON Data string that we want to update. It might be a variable or a column containing JSON 表达式 :这是我们要更新的JSON数据字符串。 它可能是变量或包含JSON的列
  • Path: We specify the property that requires an update in the JSON string. It requires the following arguments:

    路径:我们在JSON字符串中指定需要更新的属性。 它需要以下参数:

    [append] [lax | strict] $.<json path>

    [附加] [宽松| 严格] $。<json路径>

    • Append: It is an optional argument, and it specifies a new value that should be appended to the array 追加:这是一个可选参数,它指定了应该附加到数组的新值
    • Lax: it is the default mode in the path argument. Suppose we specify a property in the path argument that does not exist, in this case, the JSON_MODIFY function tries to insert the specified new value. It might give you an error message in case it cannot insert the value 宽松:这是path参数中的默认模式。 假设我们在path参数中指定了一个不存在的属性,在这种情况下,JSON_MODIFY函数尝试插入指定的新值。 万一无法插入值,可能会给您一条错误消息
    • Strict: In the strict mode, if the property we specified does not exist, it does not try to insert the value. You get an error message 严格:在严格模式下,如果我们指定的属性不存在,则它不会尝试插入该值。 您收到一条错误消息
    • Json_path: it contains the property path that we wish to update Json_path:它包含我们要更新的属性路径


  • New value: It is the new value that we require to update in the JSON

    新值:这是我们需要在JSON中更新的新值

Let’s understand the usage of the JSON_MODIFY() function using examples.

让我们使用示例来了解JSON_MODIFY()函数的用法。

示例1:更新JSON属性值 (Example 1: Update JSON property value)

In the below example, we have key-value pair for Brand and Product key. Suppose you wish to update the product value in this JSON, we can use the following code using JSON_MODIFY().

在以下示例中,我们具有“品牌”和“产品”键的键/值对。 假设您希望更新此JSON中的产品值,我们可以使用以下代码使用JSON_MODIFY()。

SELECT JSON_MODIFY('{"Brand":"HP","Product":"Laptop"}', '$.Product', 'Laptop') AS 'Updated JSON';

It returns the updated JSON string in the output.

它在输出中返回更新的JSON字符串。

In this example, note the following things.

在此示例中,请注意以下事项。

  • First argument expression contains original JSON {“Brand”:”HP”,”Product”:”Laptop”}
  • 第一个参数表达式包含原始JSON {“ Brand”:“ HP”,“ Product”:“ Laptop”}
  • $.Product is the property path that we want to update
  • $ .Product是我们要更新的属性路径
  • laptop is a new value that we want to update in the $.Product key 笔记本电脑是我们要在$ .Product键中更新的新值。

示例2:获取原始和更新的JSON数据 (Example 2: Get original and updated JSON data)

Suppose for comparison, we want both original (before the update) and updated (after update) JSON data in the output. In this query, we declared a variable and stored JSON into it. Later, we used JSON_MODIFY() function to get the updated JSON.

为了进行比较,我们希望输出中同时包含原始(更新之前)和更新(更新之后)JSON数据。 在此查询中,我们声明了一个变量并将JSON存储到其中。 稍后,我们使用JSON_MODIFY()函数获取更新的JSON。

DECLARE @OriginalJSON NVARCHAR(4000)
Set @OriginalJSON='{"Brand":"HP","Product":"Laptop"}'
Select
        @OriginalJSON as 'Before Update',
        JSON_MODIFY(@OriginalJSON,'$.Product', 'Laptop') AS 'Updated JSON';

In the output, we get both original and updated JSON.

在输出中,我们同时获取原始和更新的JSON。

In the below query, note the following:

在下面的查询中,请注意以下几点:

  • Variable @path contains the required value for the update

    变量@path包含更新所需的值
  • Specify variable in the JSON_MODIFY() function

    在JSON_MODIFY()函数中指定变量
DECLARE @OriginalJSON NVARCHAR(4000), @newvalue varchar(30),@path varchar(20)
Set @OriginalJSON='{"Brand":"HP","Product":"Laptp"}'
Set @newvalue='Laptop'
set @path='$.Product'
Select
        @OriginalJSON as 'Before Update',
        JSON_MODIFY(@OriginalJSON,@path, @newvalue) AS 'Updated JSON';

示例3:添加一个新的JSON属性 (Example 3: Add a new JSON property)

In this example, we specify a new property $.Quantity along with its value. As we know, by default, JSON_MODIFY() function uses lax path mode, so it inserts this new property because it does not exist in the original JSON string.

在此示例中,我们指定一个新属性$ .Quantity及其值。 众所周知,默认情况下,JSON_MODIFY()函数使用宽松的路径模式,因此会插入此新属性,因为它在原始JSON字符串中不存在。

DECLARE @OriginalJSON NVARCHAR(4000)
Set @OriginalJSON='{"Brand":"HP","Product":"Laptop"}'
Select
        @OriginalJSON as 'Before Update',
        JSON_MODIFY(@OriginalJSON,'$.Quantity',10) AS 'Updated JSON';

You can see it inserts the property using JSON_MODIFY() function if the property does not exist.

您可以看到它使用JSON_MODIFY()函数插入该属性(如果该属性不存在)。

Let’s specify strict path mode for the above example, and it does not insert the property in JSON as it does not exist.

让我们为上述示例指定严格路径模式,由于该属性不存在,因此不会在JSON中插入该属性。

DECLARE @OriginalJSON NVARCHAR(4000)
Set @OriginalJSON='{"Brand":"HP","Product":"Laptop"}'
Select
        @OriginalJSON as 'Before Update',
        JSON_MODIFY(@OriginalJSON,'strict$.Quantity',10) AS 'Updated JSON';

示例4:添加一个包含数组的新JSON属性 (Example 4: Add a new JSON property containing an Array)

In the previous example, we added a new property that contains a value. We can have an array in the JSON as well.

在前面的示例中,我们添加了一个包含值的新属性。 我们也可以在JSON中有一个数组。

In the below query, we defined a variable @DataArray and defined an array to have multiple values.

在下面的查询中,我们定义了一个变量@DataArray并定义了一个具有多个值的数组。

DECLARE @OriginalJSON NVARCHAR(4000)
DECLARE @DataArray NVARCHAR(256) = N'["Keyboard","Mouse","Monitor"]';
Set @OriginalJSON='{"Brand":"HP","Product":"Laptop"}'
Select
        @OriginalJSON as 'Before Update',
        JSON_MODIFY(@OriginalJSON,'$.Accessories',@DataArray) AS 'Updated JSON';

In the output, we verify that it adds a new node in JSON data, but we see a backslash symbol for each element in the array. It is not the desired JSON array. We need to use another JSON function JSON_QUERY() to insert an array using the JSON_MODIFY() function.

在输出中,我们确认它在JSON数据中添加了一个新节点,但是我们在数组中的每个元素上看到一个反斜杠符号。 它不是所需的JSON数组。 我们需要使用另一个JSON函数JSON_QUERY()来使用JSON_MODIFY()函数插入数组。

DECLARE @OriginalJSON NVARCHAR(4000)
DECLARE @DataArray NVARCHAR(256) = N'["Keyboard","Mouse","Monitor"]';
Set @OriginalJSON='{"Brand":"HP","Product":"Laptop"}'
Select
        @OriginalJSON as 'Before Update',
        JSON_MODIFY(@OriginalJSON,'$.Accessories',JSON_Query(@DataArray)) AS 'Updated JSON';

We get the property array in the output after using the JSON_QUERY() function.

使用JSON_QUERY()函数后,我们在输出中获得属性数组。

示例5:将值附加到JSON数组 (Example 5: Append a value to JSON array)

In the previous example, we inserted a new array in the JSON string. Suppose we want to insert a new value in the existing JSON array. We can use JSON_MODIFY() function for this as well.

在前面的示例中,我们在JSON字符串中插入了一个新数组。 假设我们要在现有的JSON数组中插入一个新值。 我们也可以为此使用JSON_MODIFY()函数。

In the below query, we insert Printer in the accessories array. For this purpose, we use argument Append before the array property.

在以下查询中,我们将Printers插入附件阵列。 为此,我们在数组属性之前使用参数Append

DECLARE @OriginalJSON NVARCHAR(4000)
Set @OriginalJSON='{"Brand":"HP","Product":"Laptop","Accessories":["Keyboard","Mouse","Monitor"]}'
Select
        @OriginalJSON as 'Before Update',
        JSON_MODIFY(@OriginalJSON,'append $.Accessories','Printer') AS 'Updated JSON';

You can look for additional value in the updated JSON.

您可以在更新的JSON中查找其他值。

示例6:在JSON数据中附加一个JSON对象 (Example 6: Append a JSON object in the JSON data)

We can have a nested JSON object as well inside a JSON. For example, a JSON string can contain another JSON string in its property.

我们可以在JSON中也有嵌套的JSON对象。 例如,一个JSON字符串可以在其属性中包含另一个JSON字符串。

For example, suppose we want to add a JSON object that contains seller information in the existing JSON. We need to specify the new JSON in the third parameter. For simplicity purposes, it’s better to declare a variable and contain JSON into it similar to the below code.

例如,假设我们要在现有JSON中添加一个包含卖方信息的JSON对象。 我们需要在第三个参数中指定新的JSON。 为了简单起见,最好声明一个变量并将JSON包含在其中,类似于下面的代码。

DECLARE @OriginalJSON NVARCHAR(4000), @newjson VARCHAR(100);
SET @OriginalJSON = '{"Brand":"HP","Product":"Laptop","Accessories":["Keyboard","Mouse","Monitor"]}';
SET @newjson = '{"Seller":"ABC corp","Buyer":"XYZ corp"}';
SELECT @OriginalJSON AS 'Before Update', 
       JSON_MODIFY(@OriginalJSON, '$.SellerBuyer', @newjson) AS 'Updated JSON';

In the above query output, we see that SQL Server escaped double-quotes and we get backslash in the new JSON. As highlighted earlier, we can use JSON_QUERY() function and get the valid JSON output, as shown below.

在上面的查询输出中,我们看到SQL Server转义了双引号,并且在新JSON中得到了反斜杠。 如前所述,我们可以使用JSON_QUERY()函数并获取有效的JSON输出,如下所示。

DECLARE @OriginalJSON NVARCHAR(4000), @newjson VARCHAR(100);
SET @OriginalJSON = '{"Brand":"HP","Product":"Laptop","Accessories":["Keyboard","Mouse","Monitor"]}';
SET @newjson = '{"Seller":"ABC corp","Buyer":"XYZ corp"}';
SELECT @OriginalJSON AS 'Before Update', 
       JSON_MODIFY(@OriginalJSON, '$.SellerBuyer', JSON_QUERY(@newjson)) AS 'Updated JSON';

In the below code, we have an existing array [Accessories] with few values. Let’s add the new JSON into this Accessories array. For this purpose, we use append argument along with the [$. Accessories ] in the second argument. You can view the output shows JSON in the array.

在下面的代码中,我们有一个带有很少值的现有数组[ Accessories] 。 让我们将新的JSON添加到此Accessories数组中。 为此,我们将append参数与[$一起使用。 附件]中的第二个参数。 您可以查看输出显示数组中的JSON。

DECLARE @OriginalJSON NVARCHAR(4000), @newjson VARCHAR(100);
SET @OriginalJSON = '{"Brand":"HP","Product":"Laptop","Accessories":["Keyboard","Mouse","Monitor"]}';
SET @newjson = '{"Seller":"ABC corp","Buyer":"XYZ corp"}';
SELECT @OriginalJSON AS 'Before Update', 
       JSON_MODIFY(@OriginalJSON, 'append $.Accessories', JSON_QUERY(@newjson)) AS 'Updated JSON';

示例7:删除JSON属性 (Example 7: Removing a JSON property)

Sometimes, we may want to remove an existing JSON property. To remove a JSON node or property, pass the NULL value in the third argument. We can also remove a specific value from the JSON array.

有时,我们可能想删除现有的JSON属性。 要删除JSON节点或属性,请在第三个参数中传递NULL值。 我们还可以从JSON数组中删除特定值。

In the below query, we want to remove the keyboard from the Accessories array. We specify the following in the query.

在下面的查询中,我们要从配件阵列中删除键盘。 我们在查询中指定以下内容。

  • $.Accessories[0] $ .Accessories [0]
  • Specify a NULL value in the third argument

    在第三个参数中指定一个NULL值
DECLARE @OriginalJSON NVARCHAR(4000), @newjson VARCHAR(100);
SET @OriginalJSON = '{"Brand":"HP","Product":"Laptop","Accessories":["Keyboard","Mouse","Monitor"]}';
SELECT @OriginalJSON AS 'Before Update', 
       JSON_MODIFY(@OriginalJSON, '$.Accessories[0]', NULL) AS 'Updated JSON';

Once we execute the above JSON script, it replaced the array element with NULL. Are we ok with this NULL value? No, Right!

一旦执行了上述JSON脚本,它就会将数组元素替换为NULL。 我们可以接受这个NULL值吗? 无权利!

  • JSON_MODIFY() should not replace a value with a NULL value

    JSON_MODIFY()不应将值替换为NULL值
  • It should obliterate the element

    它应该消除元素

We can replace the array with a new set of values. It helps to eliminate NULL values in the output.

我们可以用一组新值替换数组。 它有助于消除输出中的NULL值。

In the below query, we replaced the existing array with a new array that does not contain the element we do not require.

在下面的查询中,我们用不包含我们不需要的元素的新数组替换了现有数组。

DECLARE @OriginalJSON NVARCHAR(4000), @newjson VARCHAR(100);
SET @OriginalJSON = '{"Brand":"HP","Product":"Laptop","Accessories":["Keyboard","Mouse","Monitor"]}';
set @newjson='["Mouse","Monitor"]'
SELECT @OriginalJSON AS 'Before Update', 
       JSON_MODIFY(@OriginalJSON, '$.Accessories', JSON_Query(@newjson)) AS 'Updated JSON';

In the output, we can verify that replacing an array solved the problem.

在输出中,我们可以验证替换数组是否可以解决问题。

In the below query, we remove Products property from the JSON data. We do not get NULL value in the output in this case, and it removed the node as shown below.

在下面的查询中,我们从JSON数据中删除Products属性。 在这种情况下,我们不会在输出中获得NULL值,它如下所示删除了该节点。

DECLARE @OriginalJSON NVARCHAR(4000), @newjson VARCHAR(100);
SET @OriginalJSON = '{"Brand":"HP","Product":"Laptop","Accessories":["Keyboard","Mouse","Monitor"]}';
SELECT @OriginalJSON AS 'Before Update', 
       JSON_MODIFY(@OriginalJSON, '$.Product', NULL) AS 'Updated JSON';

示例8:更新一个JSON属性 (Example 8: Update a JSON property)

We can update an existing property using the JSON_MODIFY() function as well. For example, suppose someone entered the wrong value in the Accessories array in the zero position. The below code updates the element with a new specified value.

我们也可以使用JSON_MODIFY()函数更新现有属性。 例如,假设有人在“附件”数组中的零位置输入了错误的值。 以下代码使用新的指定值更新元素。

DECLARE @OriginalJSON NVARCHAR(4000), @newjson VARCHAR(100);
SET @OriginalJSON = '{"Brand":"HP","Product":"Laptop","Accessories":["Keyboard","Mouse","Monitor"]}';
set @newjson='Printer'
SELECT @OriginalJSON AS 'Before Update', 
       JSON_MODIFY(@OriginalJSON, '$.Accessories[0]', @newjson) AS 'Updated JSON';

Imagine what is the output of the below code. Here, we try to update an array element, but the array does not exist in the JSON data.

想象以下代码的输出是什么。 在这里,我们尝试更新一个数组元素,但是该数组在JSON数据中不存在。

DECLARE @OriginalJSON NVARCHAR(4000), @newjson VARCHAR(100);
SET @OriginalJSON = '{"Brand":"HP","Product":"Laptop","Accessories":["Keyboard","Mouse","Monitor"]}';
set @newjson='Printer'
SELECT @OriginalJSON AS 'Before Update', 
       JSON_MODIFY(@OriginalJSON, '$.Company[0]', @newjson) AS 'Updated JSON';

We do not get any error message. It shows similar JSON in both original and updated JSON. You should recall here that by default, JSON_MODIFY() function uses lax path mode.

我们没有收到任何错误消息。 它在原始和更新的JSON中都显示类似的JSON。 您应该在这里回忆起,默认情况下,JSON_MODIFY()函数使用宽松路径模式。

If we rerun the previous code with a small change. We modified path mode from lax to strict. This time it does not work and complains about invalid property in the JSON path.

如果我们稍做改动就重新运行先前的代码。 我们将路径模式从宽松改为严格。 这次它不起作用,并抱怨JSON路径中的无效属性。

DECLARE @OriginalJSON NVARCHAR(4000), @newjson VARCHAR(100);
SET @OriginalJSON = '{"Brand":"HP","Product":"Laptop","Accessories":["Keyboard","Mouse","Monitor"]}';
set @newjson='Printer'
SELECT @OriginalJSON AS 'Before Update', 
       JSON_MODIFY(@OriginalJSON, '$.Company[0]', @newjson) AS 'Updated JSON';

示例9:重命名密钥 (Example 9: Rename a Key)

We have seen many use cases of JSON_MODIFY() function in the previous examples. Suppose, you want to rename a key available in the JSON Data. It is like rename an existing column in a relational database table.

在前面的示例中,我们已经看到了JSON_MODIFY()函数的许多用例。 假设您想重命名JSON数据中可用的密钥。 就像重命名关系数据库表中的现有列一样。

In the below code, we use Nested JSON_MODIFY() functions along with a combination of JSON_VALUE function. Look at the query carefully. We created a new key and dropped the existing key after copying the value in it. It is a little complicated, but JSON_MODIFY() does not have a direct option to rename a key.

在下面的代码中,我们使用嵌套的JSON_MODIFY()函数以及JSON_VALUE函数的组合。 仔细查看查询。 我们创建了一个新密钥,并在复制其中的值后删除了现有密钥。 这有点复杂,但是JSON_MODIFY()没有直接选择重命名密钥的选项。

DECLARE @OriginalJSON NVARCHAR(4000)
SET @OriginalJSON = '{"Brand":"HP","Product":"Laptop"}';
SELECT @OriginalJSON AS 'Before Update', 
       JSON_MODIFY(
       JSON_MODIFY(@OriginalJSON, '$.Parts', JSON_VALUE(@OriginalJSON,'$.Product')),
       '$.Product',NULL);

示例10:多项更改 (Example 10: Multiple changes)

We can perform multiple changes in the JSON data. JSON_MODIFY() does not support multiple updates in a single function call. To overcome this problem, we can use nested JSON_MODIFY() functions.

我们可以在JSON数据中执行多项更改。 JSON_MODIFY()在单个函数调用中不支持多个更新。 为了克服这个问题,我们可以使用嵌套的JSON_MODIFY()函数。

In the below query, we update the following values.

在下面的查询中,我们更新以下值。

  • It updates the contents of the JSON array

    它更新JSON数组的内容
  • It updates the value for the Product key

    它更新产品密钥的值
DECLARE @OriginalJSON NVARCHAR(4000), @newjson VARCHAR(100);
SET @OriginalJSON = '{"Brand":"HP","Product":"Laptop","Accessories":["Keyboard","Mouse","Monitor"]}';
SET @newjson = '["HDMI","USB"]';
SELECT @OriginalJSON AS 'Source JSON', 
       JSON_MODIFY(JSON_MODIFY(@OriginalJSON, '$.Accessories', JSON_QUERY(@newjson)), '$.Brand', 'Lenovo') AS 'Updated JSON';

结论 (Conclusion)

In this article, we overviewed JSON_MODIFY() function and its usage to update JSON data using various examples. I hope it should be a knowledge article for you to understand the JSON function – JSON_MODIFY().

在本文中,我们使用各种示例概述了JSON_MODIFY()函数及其在更新JSON数据中的用法。 我希望这应该是一本知识文章,让您了解JSON函数– JSON_MODIFY()。

翻译自: https://www.sqlshack.com/modifying-json-data-using-json_modify-in-sql-server/

modify sql

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

modify sql_在SQL Server中使用JSON_MODIFY()修改JSON数据 的相关文章

  • SQL CLR 内的 WCF 客户端

    我知道它不受支持 而且我知道这甚至不是一个好主意 但是 我希望在 SQL 表值函数中拥有一个 WCF 客户端 我 看似 注册了正确的程序集 但在运行我的客户端时 我收到 WCF 错误 Msg 6522 Level 16 State 1 Li
  • 对于我的智力来说,太多的 order by、max、子查询

    我似乎无法解决这个问题 我确信它需要子查询 但我没有选择 我的大脑无法处理这个或其他事情 我需要帮助 小介绍 我有一个投注赔率网站 每 15 分钟 我都会从不同的博彩公司导入特定赛事的最新赔率 赢 平 输 或 1 X 2 赔率表的每一行都有
  • 带汇总总计和小计

    我有一个脚本可以生成几乎已经存在的结果集 我正在尝试获取小计和总计 我在年份栏中得到了小计 在最后得到了总计 我的目标是让最终结果显示 总计 而不是小计 请注意 由于汇总函数 我的最后一行 位置 也返回为空 SELECT YEAR COUN
  • 我可以使用 DataContract 序列化程序序列化 Dictionary 吗?

    我计划构建一个 WCF 服务 返回序列化为 JSON 的通用字典对象 不幸的是 序列化失败 因为对象可能总是不同的 KnownTypes 没有帮助 因为属性类型是 Dictionary 而且我不能说 KnownType 因为类可能总是不同
  • Hibernate OneToMany 列表中的重复结果

    我已将 1 N 关系与 OneToMany 列表映射 但当我访问该列表时 由于 OUTER JOIN 结果会重复 映射如下所示 Entity public class Programmer ElementCollection fetch F
  • 如何在 PL/SQL 中将列添加到现有表之前检查列是否存在?

    在向 Oracle 数据库的表中添加列之前 如何添加简单的检查 我已经包含了用于添加列的 SQL ALTER TABLE db tablename ADD columnname NVARCHAR2 30 可以使用以下视图之一访问有关 Ora
  • 如何在sql server中获取从当前日期时间到过去7天的过去7天的数据

    您好 我正在使用 pentaho 将表 A 数据从 sql server 加载到 mysql 加载数据时 我只需要从 sql server A 表获取最近 7 天的数据到 mysql 在sql server中createddate列数据类型
  • 网络抓取未知数据结构(JSON、嵌套列表或其他什么?)

    我构建了一个网络抓取工具this https campus datacamp com courses intro to python for data science chapter 1 python basics该页面取决于将字符串解析为
  • 将多嵌套 dict/json 加载到 pandas 中

    我正在尝试加载一个非常令人困惑的多重嵌套JSON变成熊猫 我已经在使用了json 规范化 http pandas pydata org pandas docs stable reference api pandas io json json
  • 如何更改 localStorage 项中的单个值?

    我正在尝试更改本地存储中的值 此项是复选框的状态 我希望 每次选中复选框时都将该复选框的值设置为 true 或 false 我尝试了很多方法 直到我意识到不使用 JSON 就无法更改值 要添加我使用的值 localStorage setIt
  • 何时在 SQL 语句中使用单引号?

    我知道当我处理 TEXT 类型的数据时应该使用它 我猜是那些回退到 TEXT 的数据 但这是唯一的情况吗 Example UPDATE names SET name Mike WHERE id 3 我正在用 C 编写 SQL 查询自动生成
  • Rust Json 序列化重叠职责

    我正在学习 Rust 中的 Json 序列化 特别是如何将 Rust 对象序列化为 Json 目前我看到 3 种将结构体实例转换为 Json 的方法 派生可编码特征 手动实现 ToJson 特征 手动实现可编码特征 下面的代码说明了所有 3
  • 使用一条语句在 MySQL 中添加多列

    我试图将多个列添加到 phpMyAdmin 中的现有表中 但我不断收到相同的错误 1064 你的 SQL 语法有错误 检查与您的 MySQL 服务器版本相对应的手册以获取正确的语法 我在写信 ALTER TABLE WeatherCente
  • 在mysql中使用GROUP BY时,如何选择最长的文本字段,如MAX()?

    在 MySql 中 您可以使用MAX 使用时获得最高值的函数GROUP BY 我怎样才能做同样的事情来获得最长的文本字符串 样本表 id post id title body 1 ZXBF1J Favorite Color My favor
  • 比较字符串结尾的最佳方法是使用 RIGHT、LIKE 还是其他?

    我需要将字符串的结尾与存储过程中可能的结尾列表进行比较 会被叫很多 大概有10 15个候选结局 此时 仅使用代码的解决方案比创建专用于此的表更好 类似的东西 IF ENDSWITH var foo OR ENDSWITH var bar O
  • 如何通过csv文件仅更新sql表的一列

    我有一个 csv 文件包含一些数据 在我的 Sql 数据库中 我有一个具有多个列名的表 现在我只想通过 csv 文件更新一列 谢谢 你可以这样尝试 Import the csv file to a temp table Update you
  • Jackson序列化配置

    我在 Spring 3 MVC 应用程序中使用 Jackson JSON 为了不序列化每个日期字段 我创建了一个使用特定日期格式的自定义对象映射器 Component jacksonObjectMapper public class Cus
  • 无法绑定多部分标识符

    我在 SO 上看到过类似的错误 但我找不到解决我的问题的方法 我有一个 SQL 查询 例如 SELECT DISTINCT a maxa b mahuyen a tenxa b tenhuyen ISNULL dkcd tong 0 AS
  • 正则表达式中 (*) 和 .* 有什么区别? [复制]

    这个问题在这里已经有答案了 是任意字符零次或多次 我试图找到以元音开头的单词 我用了 aeiou 它给了我所有以元音开头的单词 当我这样做时给出相同的结果 aeiou 现在我正在寻找以元音结尾的单词 我做到了 aeiou 它没有给出任何结果
  • 将列从日期转换为日期时间

    我有一个名为Lastmodified 数据类型为Date 但本来应该是DateTime 有没有办法转换列 当我使用 SQL Server Management Studio 的 设计 功能时 出现以下错误 不允许保存更改 您所做的更改需要以

随机推荐