Question

Sorry to pester with a simple problem but I'm stumped with a simple select on an HTML5 WebSQL Data Base.

Table tPhones has id, hid, location and several other columns. I would like to return a list of rows with where hid = [input value] or, if no rows with that hid exist, return rows where hid = 1.

I have tried LIMIT 1 ACS etc but that too fails.

Function showPhones(){
        var phonegroup = $(this).attr("id");
        var hid = localStorage.hid;
        db.transaction (function(transaction)
        {
            var sql = "SELECT * FROM tPhones WHERE dept ='"+phonegroup+"' AND ((hid ='"+hid+"') OR IFNULL (hid ='1')) ORDER BY location ASC";           
        transaction.executeSql (sql, undefined,function(transaction,result)
            {…………..rest of function working fine

Any help would be much appreciated.

Was it helpful?

Solution

A normal expression in the WHERE clause can access only columns of the current record.

To check for the existence of any other record, you have to use a subquery:

SELECT *
FROM tPhones
WHERE dept = ?
  AND (hid = ? OR (hid = 1 AND
                   NOT EXISTS (SELECT 1
                               FROM tPhones
                               WHERE dept = ?
                                 AND hid = ?)))
ORDER BY location
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top