Joining and filtering tables on a nested field (in a JSON array)
Joining and filtering tables on a nested field (in a JSON array)
I have a server-side script that joins several tables on simple columns and then filters by various criteria. So far, so good. But one of my columns contains a JSON array and I need to be able to filter on a value in that array.
a) Is it possible to join two tables like this: [code]
LEFT JOIN
$sTable2
ON ($sTable2.column1['field'] = $sTable1.column1)
[/code]
b) How do I write the associated filter, e.g:
[code]{
$sWhere .= "(";
$sWhere .= $aColumns[1]['field']." = ".$mycriteria;
$sWhere .= ") AND ";
} [/code]
Any help much appreciated.
a) Is it possible to join two tables like this: [code]
LEFT JOIN
$sTable2
ON ($sTable2.column1['field'] = $sTable1.column1)
[/code]
b) How do I write the associated filter, e.g:
[code]{
$sWhere .= "(";
$sWhere .= $aColumns[1]['field']." = ".$mycriteria;
$sWhere .= ") AND ";
} [/code]
Any help much appreciated.
This discussion has been closed.
Replies
Does the SQL engine that you are using support being able to access JSON objects inside the query? I know that Postgres 9.2 added JSON support, but that was only as a data type - it can't (as far as I know) actually access the JSON properties. I'm not aware of any SQL databases which do operate like that - some of the No-SQL dbs do I think, but I'm much more of a Javascripter than SQL fella!
Unless your SQL engine does support this, then you would need to bring the data into the application space and do the filtering there - at which point you will loose the benefits of server-side processing. I'm afraid at that point you would need to consider altering the SQL schema.
Allan
[code]
while ( $aRow = mysql_fetch_array( $rResult ) )
{
$row = array();
for ( $i=0 ; $i
For example, lets say you have 10 records per page. If you were to simply pull out 10 records from the db, you might discard 6 of them. You can't just return the 4 remaining rows to the client-side, since it is expecting 10 rows. So you'd need to do another pull from the server, where the same issue might occur again. And that is compounded by the fact that its not just the 10 rows on the current page you need to know about, you need to know how many rows are in the result set total, otherwise pagination will be buggy.
So assuming the database you are using can't read JSON properties, I think you have two choices:
1. Alter the schema to make the query you want to perform possible
2. Do everything in client-side processing or in the server-side application space.
Allan
Means reworking a number of data connections but is probably the best long-term solution.
Allan