如果不存在,Sqlite选择默认行

时间:2014-03-23 12:14:15

标签: sqlite select default

很抱歉解决了一个简单的问题,但我很难在HTML5 WebSQL数据库上进行简单的选择。

tPhones包含idhidlocation和其他几列。我想返回包含where hid = [input value]的行列表,如果不存在hid的行,则返回行where hid = 1

我尝试了LIMIT 1 ACS等,但也失败了。

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

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

WHERE子句中的普通表达式只能访问当前记录的列。

要检查是否存在任何其他记录,您必须使用子查询:

SELECT *
FROM tPhones
WHERE dept = ?
  AND (hid = ? OR (hid = 1 AND
                   NOT EXISTS (SELECT 1
                               FROM tPhones
                               WHERE dept = ?
                                 AND hid = ?)))
ORDER BY location