[英]How do I join MySQL tables with JSON fields?
我有兩個要執行聯接查詢的表。
傳感器是一個表,其中的記錄涉及傳感器元數據。 組合表是根據一些用戶定義的公式將多個傳感器數據組合為一個表的表格。
傳感器:
| sensor_id | attribute | operating_voltage | ... |
| 1 | temp | 5V | ... |
| 2 | humidity | 5V | ... |
| 3 | count | 5V | ... |
| 4 | count | 5V | ... |
組合:
| combination_id | formula | formula_mapping | ... |
| 1 | x + y | { x: 3, y: 4 } | ... |
| 2 | x * y | { x: 1, y: 3 } | ... |
我想選擇所有組合,並與在groups.formula_mapping列中定義的JSON鍵引用的傳感器進行內部聯接。 我嘗試使用
SELECT * FROM combinations
INNER JOIN sensors
ON json_contains(combinations.formula_mapping, sensors.sensor_id, '$.*');
但是,似乎不允許使用通配符*。 所有其他方法JSON_KEYS,JSON_EXTRACT等似乎都需要為查詢定義特定的路徑。 是否可以使用上述模式執行1個聯接查詢? 還是在執行其他查詢之前先查詢組合?
編輯以包括預期的查詢結果:
| combination_id | sensor_id |
| 1 | 3 |
| 1 | 4 |
| 2 | 1 |
| 2 | 3 |
您可以改用Json_Search()
函數:
SELECT * FROM combinations AS c
INNER JOIN sensors AS s
ON JSON_SEARCH(c.formula_mapping, 'one', s.sensor_id) IS NOT NULL
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.