5

単純なDBクエリを機能させようとしていますが、句clojure/java.jdbcから選択できません。IN

コードは次のようになります。

(sql/with-connection db
  (sql/with-query-results rows
    ["select  f.name        name
          ,   f.id          file_id
      from    FileCategory  fc
      join    File          f
          on  fc.file       = f.id
      where   fc.category   in ?
      having  count(1)      >= ?"
     [1 2]    ; This is the bit which does not work.
              ; I have tried (to-array) and (set) too
     2]
    (into [] rows)))

セットをクエリに渡す方法についてのアイデアはありますか?

私の直下でクエリを実行してmysqlも問題はありません。

mysql> select f.name, f.id from FileCategory fc join File f on fc.file = f.id where fc.category in (1, 2) having count(1) >= 2;
+-----------+----+
| name      | id |
+-----------+----+
| some name |  1 |
+-----------+----+
1 row in set (0.02 sec)

mysql> 

違いが生じる場合は、org.clojure / clojure 1.4.0、org.clojure / java.jdbc 0.2.3、およびmysql /mysql-connector-java5.1.6を使用しています。

4

2 に答える 2

0

これを試して:

(sql/with-connection db
  (sql/with-query-results rows
    ["select  f.name        name
          ,   f.id          file_id
      from    FileCategory  fc
      join    File          f
          on  fc.file       = f.id
      where   fc.category   in (?, ?)
      having  count(1)      >= ?"
     1 2 2]
    (into [] rows)))
于 2013-03-22T21:59:51.260 に答える
0

SQLを使用する場合は、正しい数の「?」を使用してSQLクエリを生成する必要があります。不幸にも。

より高いレベルの抽象化で作業する方がうまくいくことがわかるかもしれません。例えば。コルマでのクエリは次のようになります。

(defentity file)
(defentity filecategory)

(def categories ["movie" "tv" "news"])

(as-sql (select file
       (fields :name :file_id)
       (join filecategory (= :file.id :filecategory.file))
           (where { :filecategory.category [in categories]} )
       (having (> (sqlfn :count 1) 1))))

; SELECT `file`.`name`, `file`.`file_id` FROM `file` LEFT JOIN 
;        `filecategory` ON `file`.`id` = `filecategory`.`file` 
;         WHERE (`filecategory`.`category` IN (?, ?, ?)) 
;         HAVING COUNT(?) > ?  ::  [tv movie news 1 1]    

defentity必要に応じて、FK宣言を呼び出しに移動することで、これをさらに整理できます。

于 2013-03-23T07:49:55.910 に答える