sqlite 中的嵌套语句
我正在使用 c++ 中的 sqlite3 库从 *.sqlite 文件查询数据库。你可以在sqlite3中编写一个查询语句吗:
char* sql = "select name from table id = (select full_name from second_table where column = 4);"
第二个语句应该返回一个id来完成第一个语句的查询语句。
I'm using the sqlite3 library in c++ to query the database from *.sqlite file. can you write a query statement in sqlite3 like:
char* sql = "select name from table id = (select full_name from second_table where column = 4);"
The second statement should return an id to complete the query statement with first statement.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,您可以,只需确保嵌套查询不会返回超过一行。将 LIMIT 1 添加到嵌套查询的末尾以解决此问题。还要确保它始终返回一行,否则主查询将无法工作。
如果要匹配嵌套查询中的多行,则可以使用
IN
,如下所示:或者可以使用
JOIN
:请注意
IN< /code> 方法可能非常慢,而如果您在右侧列上建立索引,则
JOIN
可能非常快Yes you can, just make sure that the nested query doesn't return more than one row. Add a LIMIT 1 to the end of the nested query to fix this. Also make sure that it always returns a row, or else the main query will not work.
If you want to match several rows in the nested query, then you can use either
IN
, like so:or you can use
JOIN
:Note that the
IN
method can be very slow, and thatJOIN
can be very fast, if you index on the right columns另外,您可以使用 SQLiteadmin (http://sqliteadmin.orbmu2k.de/) 查看数据库并直接在其中进行查询(对于测试等有用)。
On a sidenote, you can use SQLiteadmin (http://sqliteadmin.orbmu2k.de/) to view the database and make queries directly in it (useful for testing etc).