【问题标题】:Mysql compare two tables and match some of the columns value and extract dataMysql比较两个表并匹配一些列的值并提取数据
【发布时间】:2023-03-26 10:11:02
【问题描述】:

这对我来说似乎很棘手,但就是这样。我有两个名为xp_guru_propertiesxp_pn_resale 的表。在xp_pn_resale 表中,我得到了blocktype 的列,在xp_guru_properties 表中,我得到了property_namejson 的列。我想匹配这两列,并从xp_guru_properties 中获取json 列中的具体数据。以下是一些示例数据,以免您感到困惑

xp_pn_resale

xp_guru_properties,这次我查了一个数据参考xp_pn_resale表

表格中的json 字段

{
  "id": 8174,
  "typeCode": "HDB",
  "name": "111 Lengkong Tiga",
  "newLaunch": false,
  "description": "111 Lengkong Tiga",
  "totalUnits": null,
  "floors": 0,
  "topMonth": null,
  "topYear": 1988,
  "tenure": "L99",
  "coverImageId": 3196890,
  "districtCode": "D14",
  "postcode": "410111",
  "streetname": "Lengkong Tiga",
  "streetname2": null,
  "streetnumber": "111",
  "longitude": 103.9114385,
  "latitude": 1.324030239,

}

每当我从xp_resaleguru_properties 表得到匹配时,我只想从json 中获取或提取postcode。就像我想从两个表中匹配111 Lengkong Tega 那么我应该得到postcode: 410111。但这似乎很难,因为我需要连接来自xp_resaleblockstreet_name 以匹配来自guru_propertiesproperty_name。有人有什么想法吗?提前致谢。急需帮助。

【问题讨论】:

    标签: mysql sql json database


    【解决方案1】:

    您似乎想要一个连接和一个 json 提取:

    select json_extract(g.json, '$.postcode') postcode
    from xp_pn_resale p
    inner join xp_guru_properties g 
        on g.property_name = concat(p.block, ' ', p.street_name)
    

    json_extract(g.json, '$.postcode')也可以写成:g.json->'$.postcode'

    【讨论】:

    • 感谢先生的回答。你能再给我解释一下吗?我需要使用like 进行比较吗?即使我连接
    • 如果我需要使用 like 关键字从 guru_properties 获取所有 postcode 怎么办?
    • @Vince:您可以将joinon 条件更改为:g.property_name like concat('%', p.street_name)
    • 是的,我做到了。多谢。我还添加了json_extract(g.json, '$.name') name 以从guru_properties 获取name,因此我得到了两列。
    • @Vince:这个查询现在是你的了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-21
    • 1970-01-01
    • 2020-07-06
    • 1970-01-01
    • 2021-11-02
    • 1970-01-01
    相关资源
    最近更新 更多