首页 > 解决方案 > Mysql比较两个表并匹配一些列的值并提取数据

问题描述

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

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,

}

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

标签: mysqlsqljsondatabase

解决方案


您似乎想要一个连接和一个 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'


推荐阅读