I am looking for the Sqlite syntax to join multiple JSON columns, in the same table. I create a table like:-
SQL = "CREATE TABLE BranchStock (branch VARCHAR, oldstock JSON, newstock JSON)"
....
I then insert a few items like:-
INSERT INTO BranchStock VALUES('Melbourne','[{"catnumber":"ABC123","instock":5},{"catnumber":"BCD321","instock":3}]','[{"catnumber":"ABC123","instock":1},{"catnumber":"BCD321","instock":5}]');
INSERT INTO BranchStock VALUES('Sydney','[{"catnumber":"ABC123","instock":7},{"catnumber":"XYZ567","instock":0}]','[{"catnumber":"ABC123","instock":5},{"catnumber":"XYZ567","instock":3}]');
....
Now I'm looking for the search sql to list which branches have the catnumber='ABC123' newstock or oldstock, and the instock amounts. Perhaps something like this:-
SELECT * FROM BranchStock
JOIN json_each(oldstock) ON json_valid(oldstock)
JOIN json_each(newstock) ON json_valid(newstock)
WHERE json_extract(value, '$.name')='ABC123');
The above works for one JOIN, but when I implement the second JOIN I get a syntax error.
Does anyone know the correct syntax? Thanks in advance.