Skip to content

Instantly share code, notes, and snippets.

@rossmartin
Last active December 11, 2015 19:48
Show Gist options
  • Save rossmartin/4650569 to your computer and use it in GitHub Desktop.
Save rossmartin/4650569 to your computer and use it in GitHub Desktop.
Web SQL - Combine two SELECT statements with a LIMIT using UNION
for (var i = 0; i < testResults.length; i++){
(function(i){
db.transaction(function(tx){
tx.executeSql(
"SELECT * FROM ( " +
"SELECT 'lab' AS table_name, id FROM lab_test_names " +
"WHERE organizerClassId = ? " +
"LIMIT 1 " +
" ) " +
"UNION " +
"SELECT * FROM ( " +
"SELECT 'rad' AS table_name, id FROM radiology_test_names " +
"WHERE organizerClassId = ? " +
"LIMIT 1 " +
" ) ",
[testClassId[i], testClassId[i]],
function(tx, results){
//console.log("JSON.stringify(results.rows.item(0)) ==> " + JSON.stringify(results.rows.item(0)) );
var tableName = results.rows.item(0).table_name;
if (tableName == "lab"){
console.log("this is a lab result entry");
// rest of my arithmetic below if this is a lab result...
} else { // else this is a radiology result entry
console.log("this is a radiology result entry");
// rest of my arithmetic below if this is a radiology result...
}
},
function(tx, err){
console.log("SQL Error");
if (err) {
console.log("ERROR " + err.code + ": " + err.message);
}
}
); // end of tx.executeSql
}); // end of db.transaction
})(i) // closure
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment