I have a database table that keeps a column for versioning of the entries. Client applications should be able to query the server with their current versions and IDs, and the server respond with the rows that have a higher version for each of the entries. I am not an expert on MySQL
, so I cannot see how to achieve this. I have tried various things, but I am currently far from producing anything that works efficiently.
Example:
Server data:
mysql> SELECT id, version FROM my_data;
+-----+---------+
| id | version |
+-----+---------+
| 1 | 0 |
| 2 | 1 |
| 3 | 2 | <-- The JSON data below has lower version, so this row should be selected.
| 4 | 0 |
| 5 | 1 |
| 6 | 0 |
| 7 | 1 | <-- The JSON data below has lower version, so this row should be selected.
| 8 | 1 |
| 9 | 4 | <-- The JSON data below has lower version, so this row should be selected.
| 10 | 1 |
+-----+---------+
10 rows in set (0.00 sec)
Data sent from the client:
The client then queries the server with the following data in JSON (or whatever, but I have JSON in my case). The server side is php
and I need to parse this JSON data and include it in the query somehow. This is the data the client currently contains.
{
"my_data": [
{
"id": 1,
"version": 0
},
{
"id": 2,
"version": 1
},
{
"id": 3,
"version": 0
},
{
"id": 4,
"version": 0
},
{
"id": 5,
"version": 1
},
{
"id": 6,
"version": 0
},
{
"id": 7,
"version": 0
},
{
"id": 8,
"version": 1
},
{
"id": 9,
"version": 2
},
{
"id": 10,
"version": 1
}
]
}
In this example I want the MySQL
query to return 3 rows; namely the 3 rows with id
3, 7 and 9 because the client version is lower than the server version, thus it needs to fetch some data for updating. How can I achieve this in a single, simple query? I do not want to run one query for each row, even if this is possible.
Desired result from the sample data:
The resulting data should be the rows in which the version in the database on the server side is greater than the data with the corresponding id
in the JSON data set.
mysql> <INSERT PROPER QUERY HERE>;
+-----+---------+
| id | version |
+-----+---------+
| 3 | 2 |
| 7 | 1 |
| 9 | 4 |
+-----+---------+
3 rows in set (0.00 sec)