8

I'm trying to create a query with Slick 1.0.0 that returns a row count equivalent to the following SQL statement:

SELECT COUNT(*) FROM table;

What I have so far is:

val query = for {
  row <- Table
} yield row
println(query.length)

This prints scala.slick.ast.FunctionSymbol$$anon$1@6860991f. Also, query.length appears to be of type scala.slick.lifted.Column. I cannot find a way to execute the query. All examples that I can find in the documentation and anywhere else do not operate on Column or are for ScalaQuery and do not work anymore.

What can I do to execute this?

4

3 回答 3

4

虽然我无法检查生成的 sql,但您可以通过删除 .list 来获得更短的源代码:

Query(MyTable.length).first
于 2013-07-15T19:30:46.807 回答
4

这些中的任何一个都可以解决问题:

Query(MyTable).list.length

或者

(for{mt <- MyTable} yield mt).list.length

或者

(for{mt <- MyTable} yield mt.count).first

更新:

打印 H2 数据库日志显示了最后一个查询,这看起来是最佳的:

 03:31:26.560 [main] DEBUG h2database - jdbc[2]
 /**/PreparedStatement prep10 = conn1.prepareStatement("select select count(1) from \"MYTABLE\" s5", 1003, 1007);
于 2013-02-12T18:32:05.747 回答
2

利用:

val query = for(row <- Table) yield row 
println(Query(query.count).first)

count相当于“SELECT COUNT(*) FROM Table” 。为了获得第一行也是唯一的行,您必须使用它first来获取计数。

于 2013-02-12T16:25:41.340 回答