0

我有两张表 Y 和 X。Y 的每个项目都可以有很多这样的 Xi:

Y   X
--  --
A   X1  √
B   X1
B   X2
B   X3
C   X2
D   X1
D   X2
E   X1  √

我想得到只有 X1 由tick显示的 Ys 。什么是查询?

我应该提到我的数据库是 Android Sqlite。

Select * 
From Y 
Inner join X 
on Y.id = X.Yid 
where X.id is only X1 and nothing else

我怎样才能写一个真正的查询等价于is only X1 and nothing else

4

3 回答 3

1

换句话说,你想要所有Y确实有一个X1,但没有任何其他X的。在 SQL 中:

SELECT *
FROM Y
WHERE EXISTS (SELECT X.id
              FROM X
              WHERE X.id = Y.Xid
                AND X.name = 'X1')
  AND NOT EXISTS (SELECT X.id
                  FROM X
                  WHERE X.id = Y.Xid
                    AND X.name != 'X1')

或者,第一个子查询可以写成一个连接:

SELECT Y.*
FROM Y JOIN X
       ON Y.Xid = X.id
WHERE X.name = 'X1'
  AND NOT EXISTS (SELECT X2.id
                  FROM X AS X2
                  WHERE X2.id = Y.Xid
                    AND X2.name != 'X1')
于 2013-03-05T12:20:39.010 回答
0

查询可能是:

 Select * 
  From Y 
  inner join X 
 on Y.Xid = X.id 
  where X.id = 'X1'
于 2013-03-05T11:32:08.500 回答
0
Cursor c = sampleDB.rawQuery("SELECT * FROM " +
            Y +
            " where X=x1", null);

    if (c != null ) {
        if  (c.moveToFirst()) {
            do {
                String yarray[] = c.getString(c.getColumnIndex("Y"));
            }while (c.moveToNext());
        }
    }

Now yarray[] will contain all your y values
于 2013-03-05T11:44:10.590 回答