简体   繁体   中英

How to get data from dynamic table in Mysql

I will receive data from some IOT with a JSON data. Each IOT can send different data with different structure.

I created a MYSQL script to import my data in my database.

My table data_root is like a index who refer to different tables with the content to get.

I didn't know columns of my tables likes 'relay' or 'thrusters'. In my table data_root, I inserted the 'table_name' to know which table I need to question.

Tables that I want to question, have an id_data in references to the data table.

Mysql schemas: 在此处输入图片说明

I want to create Mysql Query to get data from thrusters for example or relay but without knowing their columns.

My SQL query :

SELECT data.id_data , data.timestamp, data_root.table_name, data_root.table_name_id
FROM data, data_root, thrusters
WHERE data.id_data = data_root.id_data
AND data.id_data = thrusters.id_data
AND data.code_transmetteur = 1
GROUP BY thrusters.id

Which method can I use to get data from thrusters or relay with my schema.

Thanks for your helps,

Use MySQL 8.0.11 or newer, then you have a few options:

  • create columns containing JSON and save them as you get them
  • use JSON_TABLE() to do queries on JSON-strings
  • use any of the JSON_SEARCH() -functions to check what you received and extract the data

In your case it sounds like you should use JSON_SEARCH to check what kind of object you are getting and then JSON_TABLE to properly import it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM