使用表1中的值(字符串)来引用另一个表的列

时间:2016-05-02 22:14:32

标签: sqlite

我有两张桌子:
T1:

id  name  field serial# 
1   A1    xx    123 
2   A1    yy    123
3   A1    zz    123
4   A2    xx    124
5   A2    yy    124
6   A2    zz    124

T2:

serial#   xx  yy  zz 
123       0   1   1
124       1   0   1

我想写一个查询,结果表如下:

id  name  field serial#
1   A1    yy    123
2   A1    zz    123
3   A2    xx    124
4   A2    zz    124

我尝试编写一个看起来像的查询:

select * from T1, T2
from ...
where T2.(T1.field) <> 0  <-- This is where I never get the syntax right
...

1 个答案:

答案 0 :(得分:0)

SQLite无法动态访问列;你必须手动检查每一个:

var url = "YourURL_of_Webservice"; // example : "http://localhost:54028/webservice.asmx/getCustomers"

$.ajax({
  type: "POST",
  url: url,
  success: function (data) {
    // YOUR CODE FOR SUCCESS BODY
    console.log(data)
  },
  error: function (xmlHttpRequest, textStatus, errorThrown) {
     console.log(xmlHttpRequest.responseText);
     console.log(textStatus);
     console.log(errorThrown);
  }
});

或者,使用CASE expression

的相同算法
SELECT ...
FROM T1
JOIN T2 ON T1.serial = T2.serial
       AND ((T1.field = 'xx' AND T2.xx) OR
            (T1.field = 'yy' AND T2.yy) OR
            (T1.field = 'zz' AND T2.zz))
...