MySQL比较两个表并匹配一些列值并提取数据

时间:2020-01-08 02:58:59

标签: mysql sql json database

对我来说,这似乎很棘手,但事实确实如此。我得到了两个名为xp_guru_propertiesxp_pn_resale的表。在xp_pn_resale表中,我得到了列blocktype,在表xp_guru_properties中,我得到了列property_namejson。我想匹配两列,并从json获取列xp_guru_properties中的特定数据。以下是一些示例数据,以使您不会感到困惑

xp_pn_resale my

xp_guru_properties,这一次我搜索了一个与xp_pn_resale表相关的数据 enter image description here

表中的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,

}

只要我从postcodexp_resale表中找到匹配项,我只想从json中获取或提取guru_properties。就像我要从两个表中匹配111 Lengkong Tega一样,我应该得到postcode: 410111。但是这似乎很难,因为我需要连接block的{​​{1}}和street_name以匹配xp_resale的{​​{1}}。有人知道吗?提前致谢。急需帮助。

1 个答案:

答案 0 :(得分: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'

相关问题