GVKun编程网logo

如何在MySQL查询本身中检索存储在JSON数组中的值?(如何在mysql查询本身中检索存储在json数组中的值)

12

在这里,我们将给大家分享关于如何在MySQL查询本身中检索存储在JSON数组中的值?的知识,让您更了解如何在mysql查询本身中检索存储在json数组中的值的本质,同时也会涉及到如何更有效地ios–在

在这里,我们将给大家分享关于如何在MySQL查询本身中检索存储在JSON数组中的值?的知识,让您更了解如何在mysql查询本身中检索存储在json数组中的值的本质,同时也会涉及到如何更有效地ios – 在Swift中无法检索存储在Objective-C中的Keychain的值?、java – 从mongodb中的嵌套json数组中检索值、MySQL / SQL:使用更新后的表本身中的相关子查询进行更新、mysql json数组取值、查询的内容。

本文目录一览:

如何在MySQL查询本身中检索存储在JSON数组中的值?(如何在mysql查询本身中检索存储在json数组中的值)

如何在MySQL查询本身中检索存储在JSON数组中的值?(如何在mysql查询本身中检索存储在json数组中的值)

我有下表

product_id    product_name    image_path                             misc
----------   --------------  ------------                           ------
     1            flex        http://firstpl...      {"course_level_id":19,"group_id":"40067"}
     2           Android      http://firstpl...      {"course_level_id":20,"group_id":"40072"}

因此,如何从“杂项”列中检索product_name,image_path和仅“ group_id”值(如“ 40067”)。

我在下面的查询中尝试过,但在“杂项”列中返回1/0。

SELECT product_name,image_path,misc REGEXP '(.*\"group_id\":*)' as Misc FROM ref_products where product_id=1

有人知道该怎么做吗?

ios – 在Swift中无法检索存储在Objective-C中的Keychain的值?

ios – 在Swift中无法检索存储在Objective-C中的Keychain的值?

我遇到了钥匙扣最棘手的问题.我有一个使用加密Realm数据库的现有应用程序,加密密钥正在保存到Keychain,根据Realm的代码示例 here.

- (NSData *)getKey {
    // Identifier for our keychain entry - should be unique for your application
    static const uint8_t kKeychainIdentifier[] = "io.Realm.EncryptionExampleKey";
    NSData *tag = [[NSData alloc] initWithBytesNocopy:(void *)kKeychainIdentifier
                                               length:sizeof(kKeychainIdentifier)
                                         freeWhenDone:NO];

    // First check in the keychain for an existing key
    NSDictionary *query = @{(__bridge id)kSecclass: (__bridge id)kSecclassKey,(__bridge id)kSecAttrApplicationTag: tag,(__bridge id)kSecAttrKeySizeInBits: @512,(__bridge id)kSecReturnData: @YES};

    CFTypeRef dataRef = NULL;
    Osstatus status = SecItemcopyMatching((__bridge CFDictionaryRef)query,&dataRef);
    if (status == errSecSuccess) {
        return (__bridge NSData *)dataRef;
    }

    // No pre-existing key from this application,so generate a new one
    uint8_t buffer[64];
    status = SecRandomcopyBytes(kSecRandomDefault,64,buffer);
    NSAssert(status == 0,@"Failed to generate random bytes for key");
    NSData *keyData = [[NSData alloc] initWithBytes:buffer length:sizeof(buffer)];

    // Store the key in the keychain
    query = @{(__bridge id)kSecclass: (__bridge id)kSecclassKey,(__bridge id)kSecValueData: keyData};

    status = SecItemAdd((__bridge CFDictionaryRef)query,NULL);
    NSAssert(status == errSecSuccess,@"Failed to insert new key in the keychain");

    return keyData;
}

我正在努力将此应用程序转换为Swift,我正在尝试使用Realm的快速代码示例检索存储在Keychain中的加密密钥

func getKey() -> NSData {
    // Identifier for our keychain entry - should be unique for your application
    let keychainIdentifier = "io.Realm.EncryptionExampleKey"
    let keychainIdentifierData = keychainIdentifier.data(using: String.Encoding.utf8,allowLossyConversion: false)!

    // First check in the keychain for an existing key
    var query: [Nsstring: AnyObject] = [
        kSecclass: kSecclassKey,kSecAttrApplicationTag: keychainIdentifierData as AnyObject,kSecAttrKeySizeInBits: 512 as AnyObject,kSecReturnData: true as AnyObject
    ]

    // To avoid Swift optimization bug,should use withUnsafeMutablePointer() function to retrieve the keychain item
    // See also: https://stackoverflow.com/questions/24145838/querying-ios-keychain-using-swift/27721328#27721328
    var dataTypeRef: AnyObject?
    var status = withUnsafeMutablePointer(to: &dataTypeRef) { SecItemcopyMatching(query as CFDictionary,UnsafeMutablePointer($0)) }
    if status == errSecSuccess {
        return dataTypeRef as! NSData
    }

    // No pre-existing key from this application,so generate a new one
    let keyData = NSMutableData(length: 64)!
    let result = SecRandomcopyBytes(kSecRandomDefault,keyData.mutableBytes.bindMemory(to: UInt8.self,capacity: 64))
    assert(result == 0,"Failed to get random bytes")

    // Store the key in the keychain
    query = [
        kSecclass: kSecclassKey,kSecValueData: keyData
    ]

    status = SecItemAdd(query as CFDictionary,nil)
    assert(status == errSecSuccess,"Failed to insert the new key in the keychain")

    return keyData
}

现在的问题是Swift代码无法通过Objective-C代码返回存储在Keychain中的值.我在创建keychainIdentifierData时尝试修改Swift代码以使用NSData而不是Data,但我无法让它工作.

在我的实际项目中,SecItemcopyMatching调用返回成功状态,但dataTypeRef始终为nil,在强制转换为Data时崩溃.在我为此创建的一个小测试项目中,找不到密钥,这似乎表明问题在于将keychainIdentifier转换为keychainIdentifierData,但我现在找不到任何信息如何在语言之间保持一致.

我发现的一个线索是将标签/ keychainIdentifierData打印到控制台,在Obj-c中它是
< 696f2e52 65616c6d 2e456e63 72797074 696f6e45 78616d70 6c654b65 7900>
在斯威夫特的哪个地方
< 696f2e52 65616c6d 2e456e63 72797074 696f6e45 78616d70 6c654b65 79>

这表明密钥在语言之间不匹配,但后来我不明白为什么SecItemcopyMatching会返回成功.

有没有人对如何检索我的加密密钥有任何见解?提前致谢 :)

解决方法

Objective-C keychainIdentifier被创建为C字符串,它始终以null结尾. null终止符与字符一起编码,导致您注意到的尾随’00’被附加到结果中. Swift字符串不是以null结尾的.

要实现奇偶校验,请在创建Objective-C标识符时省略最后一个字符:

static const uint8_t kKeychainIdentifier[] = "io.Realm.EncryptionExampleKey";
NSData *tag = [[NSData alloc] initWithBytesNocopy:(void *)kKeychainIdentifier
                                           length:sizeof(kKeychainIdentifier) - 1 // <- Truncate last char
                                     freeWhenDone:NO];

或者,使用Nsstring在Objective-C中表示您的标识符:

Nsstring *keychainIdentifier = @"io.Realm.EncryptionExampleKey";
NSData *tag = [keychainIdentifier dataUsingEncoding:NSUTF8StringEncoding];

java – 从mongodb中的嵌套json数组中检索值

java – 从mongodb中的嵌套json数组中检索值

我的mongo集合具有以下格式的条目

{
    "myobj" : {
        "objList" : [
            { "location" : "Texas" },{ "location" : "Houston"},{ "name":"Sam" }
        ]
    },"category" : "cat1"
}


{
"myobj" : 
{
    "objList" : [
        { "location" : "Tennesy" },{ "location" : "NY"},{ "location" : "SF" }
    ]
},"category" : "cat2"

}

我想提取位于“休斯顿”的“**类别**”.在简单的JSON对象的情况下,我必须将其作为查询传递,如:

BasicDBObject place = new BasicDBObject();
place.put("location","Houston");

但是在嵌套JSON的情况下,我不知道如何将其作为查询传递并获得适当的类别.即如果我将我的位置作为“休斯顿”通过,那么它应该返回它的相应类别“cat1”…我希望我的问题现在清楚了….

解决方法

好的,你有你的文件:

db.coll1.insert({
    "myobj" : {
        "objList" : [
            { "location" : "Texas" },"category" : "cat1"
})

db.coll1.insert({
    "myobj" : {
        "objList" : [
            { "location" : "Tennesy" },{ "location" : "SF" }
        ]
    },"category" : "cat1"
})

现在您可以使用dot operator找到您想要的内容:

db.coll1.find({“myobj.objList.location”:“Texas”}).pretty()将返回一个具有德克萨斯州的对象

db.coll1.find({“myobj.objList.location”:“SF”}).pretty()将返回一个具有SF的对象

db.coll1.find({“myobj.objList.location”:“Houston”}).pretty()将返回两个对象

现在我希望你能用Java编写它.我从来没有使用过Java,但based on this question你可以做这样的事情.如果它不起作用,只需看看如何在java驱动程序中使用dot运算符为mongo:

DBCursor cursor = coll1.find(new BasicDBObject("myobj.objList.location","Texas"));

附:你说,你想要检索类别.以这种方式,您将需要使用投影db.coll1.find({<我提供的查询},{category:1,_id:0})

MySQL / SQL:使用更新后的表本身中的相关子查询进行更新

MySQL / SQL:使用更新后的表本身中的相关子查询进行更新

我有一个一般性的问题,我将尝试通过一个例子来解释。

假设我有一个包含以下字段的表格:“ id”,“ name”,“ category”,“ appearances”和“ ratio”

我的想法是,我有几个项目,每个项目都与一个类别相关,并且多次“出现”。比率字段应包括类别中项目出现的总数中每个项目出现的百分比。

用伪代码,我需要以下内容:

  • 对于每个类别,
    找到与该 类别 相关的项目的总外观。例如,可以用(select sum("appearances") from table group bycategory)完成

  • 对于每个项目,
    将比率值设置为项目的外观除以为上述类别找到的总和

现在,我试图通过一个更新查询来实现这一目标,但似乎无法做到这一点。我认为我应该做的是:

update Table T    set T.ratio = T.appearances /   (    select sum(S.appearances)    from Table S    where S.id = T.id    )

但是MySQL在更新列中不接受别名T,而且我也没有找到其他方法来实现这一点。

有任何想法吗?

答案1

小编典典

遵循我收到的两个答案(所有答案都没有完成,所以我写了自己的答案),最终我做了以下工作:

UPDATE Table AS targetINNER JOIN (select category, appearances_sumfrom Table T inner join (    select category as cat, sum(appearances) as appearances_sum    from Table    group by cat) as aggwhere T.category  = agg.catgroup by category) as sourceON target.category = source.categorySET target.probability = target.appearances / source.appearances_sum

它运作非常迅速。我也尝试了相关子查询,但是它慢得多(数量级),所以我坚持使用联接。

mysql json数组取值、查询

mysql json数组取值、查询

来源于网络,未验证。

数据表

id uid info 1 1001 {"name":"周年庆","status":0,"addtime":"2017-10-10"} 2 1002 [ {"name":"周年庆","status":0,"addtime":"2017-10-11"},{"name":"特价促销","status":0,"addtime":"2017-10-12"}] 3 1003 [ {"name":"特价促销","status":0,"addtime":"2017-10-12"},{"name":"国庆促销","status":0,"addtime":"2017-09-28"}]

取“特价促销”的用户数据

select * from tb
where info->''$.name'' = ''特价促销''  or JSON_CONTAINS(info->''$[*].name'', ''"特价促销"'', ''$'')

 

我们今天的关于如何在MySQL查询本身中检索存储在JSON数组中的值?如何在mysql查询本身中检索存储在json数组中的值的分享就到这里,谢谢您的阅读,如果想了解更多关于ios – 在Swift中无法检索存储在Objective-C中的Keychain的值?、java – 从mongodb中的嵌套json数组中检索值、MySQL / SQL:使用更新后的表本身中的相关子查询进行更新、mysql json数组取值、查询的相关信息,可以在本站进行搜索。

本文标签: