如何使用两个条件查询 SQLite 数据库?

发布于 2024-10-23 20:29:16 字数 320 浏览 2 评论 0原文

我有一个关于员工的表,包含 3 列,如下代码:

 db.execSQL("CREATE TABLE " + TABLE_NAME + " (" + _ID
                + " INTEGER PRIMARY KEY AUTOINCREMENT, " + DEPT
                + " TEXT NOT NULL," + NAME + " TEXT NOT NULL," + CITY + " TEXT NOT NULL);");

现在我只想显示同一部门和城市的员工(即胡志明市和销售部门的员工)。我怎样查询才能得到它?

I have a table about employee with 3 columns like following code:

 db.execSQL("CREATE TABLE " + TABLE_NAME + " (" + _ID
                + " INTEGER PRIMARY KEY AUTOINCREMENT, " + DEPT
                + " TEXT NOT NULL," + NAME + " TEXT NOT NULL," + CITY + " TEXT NOT NULL);");

now I just want to show employees in the same both DEPT and CITY(e.i both employees in HCM City and Sales department). How can I query to get it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

狼性发作 2024-10-30 20:29:16

@DienTrinh,你说得对,请注意 AND 周围的空格。那应该对你有用。

 Cursor cursor = db.query(TABLE_NAME, 
                          new String [] {_ID, NAME, DEPT, CITY }, 
                          DEPT +"=?" +" AND " + CITY +"=?", 
                          new String[] {"Sales", "HCM" }, 
                          null, null, null);

@DienTrinh, you had it right, note the spaces around AND. That should work for you.

 Cursor cursor = db.query(TABLE_NAME, 
                          new String [] {_ID, NAME, DEPT, CITY }, 
                          DEPT +"=?" +" AND " + CITY +"=?", 
                          new String[] {"Sales", "HCM" }, 
                          null, null, null);
耳根太软 2024-10-30 20:29:16

解决办法是:

SELECT * FROM Employess 
WHERE DEPT='Sales' COLLATE NOCASE AND City='HCM' COLLATE NOCASE

The solution is:

SELECT * FROM Employess 
WHERE DEPT='Sales' COLLATE NOCASE AND City='HCM' COLLATE NOCASE
不寐倦长更 2024-10-30 20:29:16
SELECT * FROM Employees WHERE DEPT='Sales' AND City='HCM'
SELECT * FROM Employees WHERE DEPT='Sales' AND City='HCM'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文