- - PR -
LEFT OUTER JOIN にWHERE USER.ID=? を含められますか?
投稿者 | 投稿内容 | ||||
---|---|---|---|---|---|
|
投稿日時: 2007-05-06 14:00
LEFT OUTER JOINでテーブルを結合した場合、
where user.id=?を含めたいのですが、 どのように書けばいいかわかりません。 例えば、 select user.id, user.name, user.entryDate, user.kind, user.contact, rentInfo.limitDate, bookSpec.title from book left outer join bookSpec on book.bookSpecId = bookSpec.id left outer join rentInfoDetail on book. id = rentInfoDetail.bookId left outer join rentInfo on rentInfoDetail.rentInfoId = rentInfo.id left outer join user on rentInfo.userId = user.id where user.id=? で実行しても、失敗します。 ご教授いただけないでしょうか? 環境:windows XP H2 database engine よろしく御願いいたします。 | ||||
|
投稿日時: 2007-05-06 15:41
失敗? エラー? 抽出されない? 意図したとおりに出力されない?<こんなのあんたしかわからんよ。 そもそもデータがなかったりしない? | ||||
|
投稿日時: 2007-05-06 23:57
「?」というのは「?」という記号? ・where user.id = '?' それとも任意の一文字? ・where user.id like '_' どちらにしても「where user.id=?」では普通のSQLでは文法エラーになりそうな気がします(H2 database engineを不知なのですが断言できませんが)。末記入さんも書かれたように「失敗します」だけでは何とも・・。 「left outer join」ということで言うと、 ・where (user.idの一致条件)or(ueser.id is null) ですかねぇ(最近書いてないので自信なし)。これで希望する条件なのかどうかはわかりませんが(汗 [ メッセージ編集済み 編集者: shimix 編集日時 2007-05-07 00:04 ] | ||||
|
投稿日時: 2007-05-07 10:09
未記入さん、Shimixさん、お返事ありがとうございました。
未熟者なので説明の仕方が悪く、迷惑をおかけしてすみません。 >失敗? >エラー? >抽出されない? >意図したとおりに出力されない?<こんなのあんたしかわからんよ。 >そもそもデータがなかったりしない? テキストボックスにIDを入力して、ボタンを押しても 何も表示されないのです。 >「?」というのは「?」という記号? >・where user.id = '?' >それとも任意の一文字? >・where user.id like '_' select book.id, bookSpec.title, book.shelfNumber, book.rowNumber from book, bookSpec where book.id=? and book.bookSpecId=bookSpec.id このSQLでうまく実行できたのでこれを真似しました。 Eclipseに出力されたログを書いときます、以下。 DEBUG 2007-05-07 09:21:44,031 [http-8080-Processor25] [measurement] start date = 2007/05/07 09:21:44 031 JST(org.seasar.teeda.core.lifecycle.impl.RestoreViewPhase) DEBUG 2007-05-07 09:21:44,687 [http-8080-Processor25] クラス(book.chura.web.rentBook.RentBookPage[rentBook_rentBookPage])のコンポーネント定義を登録します DEBUG 2007-05-07 09:21:45,093 [http-8080-Processor25] クラス(book.chura.dao.BookDao[bookDao])のコンポーネント定義を登録します DEBUG 2007-05-07 09:21:45,312 [http-8080-Processor25] クラス(book.chura.dao.RentInfoDao[rentInfoDao])のコンポーネント定義を登録します DEBUG 2007-05-07 09:21:45,515 [http-8080-Processor25] クラス(book.chura.dao.UserDao[userDao])のコンポーネント定義を登録します DEBUG 2007-05-07 09:21:45,640 [http-8080-Processor25] BEGIN book.chura.web.rentBook.RentBookPage#initialize() DEBUG 2007-05-07 09:21:45,687 [http-8080-Processor25] トランザクションを開始しました DEBUG 2007-05-07 09:21:45,687 [http-8080-Processor25] トランザクションをコミットしました DEBUG 2007-05-07 09:21:45,687 [http-8080-Processor25] END book.chura.web.rentBook.RentBookPage#initialize() : null DEBUG 2007-05-07 09:21:46,453 [http-8080-Processor25] [measurement] perform ms:2422 DEBUG 2007-05-07 09:21:46,453 [http-8080-Processor25] [measurement] end date = 2007/05/07 09:21:46 453 JST(org.seasar.teeda.core.lifecycle.impl.RestoreViewPhase) DEBUG 2007-05-07 09:21:46,453 [http-8080-Processor25] [measurement] start date = 2007/05/07 09:21:46 453 JST(org.seasar.teeda.core.lifecycle.impl.RenderResponsePhase) DEBUG 2007-05-07 09:21:46,515 [http-8080-Processor25] BEGIN book.chura.web.rentBook.RentBookPage#prerender() DEBUG 2007-05-07 09:21:46,515 [http-8080-Processor25] トランザクションを開始しました DEBUG 2007-05-07 09:21:46,515 [http-8080-Processor25] トランザクションをコミットしました DEBUG 2007-05-07 09:21:46,515 [http-8080-Processor25] END book.chura.web.rentBook.RentBookPage#prerender() : null DEBUG 2007-05-07 09:21:46,953 [http-8080-Processor25] [measurement] perform ms:500 DEBUG 2007-05-07 09:21:46,953 [http-8080-Processor25] [measurement] end date = 2007/05/07 09:21:46 953 JST(org.seasar.teeda.core.lifecycle.impl.RenderResponsePhase) DEBUG 2007-05-07 09:22:26,578 [http-8080-Processor24] [measurement] start date = 2007/05/07 09:22:26 578 JST(org.seasar.teeda.core.lifecycle.impl.RestoreViewPhase) DEBUG 2007-05-07 09:22:26,734 [http-8080-Processor24] [measurement] perform ms:156 DEBUG 2007-05-07 09:22:26,734 [http-8080-Processor24] [measurement] end date = 2007/05/07 09:22:26 734 JST(org.seasar.teeda.core.lifecycle.impl.RestoreViewPhase) DEBUG 2007-05-07 09:22:26,734 [http-8080-Processor24] [measurement] start date = 2007/05/07 09:22:26 734 JST(org.seasar.teeda.core.lifecycle.impl.ApplyRequestValuesPhase) DEBUG 2007-05-07 09:22:26,859 [http-8080-Processor24] [measurement] perform ms:125 DEBUG 2007-05-07 09:22:26,859 [http-8080-Processor24] [measurement] end date = 2007/05/07 09:22:26 859 JST(org.seasar.teeda.core.lifecycle.impl.ApplyRequestValuesPhase) DEBUG 2007-05-07 09:22:26,859 [http-8080-Processor24] [measurement] start date = 2007/05/07 09:22:26 859 JST(org.seasar.teeda.core.lifecycle.impl.ProcessValidationsPhase) DEBUG 2007-05-07 09:22:26,875 [http-8080-Processor24] クラス(book.chura.web.rentBook.RentBookPage[rentBook_rentBookPage])のコンポーネント定義を登録します DEBUG 2007-05-07 09:22:27,015 [http-8080-Processor24] クラス(book.chura.dao.BookDao[bookDao])のコンポーネント定義を登録します DEBUG 2007-05-07 09:22:27,250 [http-8080-Processor24] クラス(book.chura.dao.RentInfoDao[rentInfoDao])のコンポーネント定義を登録します DEBUG 2007-05-07 09:22:27,328 [http-8080-Processor24] クラス(book.chura.dao.UserDao[userDao])のコンポーネント定義を登録します DEBUG 2007-05-07 09:22:27,484 [http-8080-Processor24] [measurement] perform ms:625 DEBUG 2007-05-07 09:22:27,484 [http-8080-Processor24] [measurement] end date = 2007/05/07 09:22:27 484 JST(org.seasar.teeda.core.lifecycle.impl.ProcessValidationsPhase) DEBUG 2007-05-07 09:22:27,484 [http-8080-Processor24] [measurement] start date = 2007/05/07 09:22:27 484 JST(org.seasar.teeda.core.lifecycle.impl.UpdateModelValuesPhase) DEBUG 2007-05-07 09:22:27,500 [http-8080-Processor24] [measurement] perform ms:16 DEBUG 2007-05-07 09:22:27,500 [http-8080-Processor24] [measurement] end date = 2007/05/07 09:22:27 500 JST(org.seasar.teeda.core.lifecycle.impl.UpdateModelValuesPhase) DEBUG 2007-05-07 09:22:27,500 [http-8080-Processor24] [measurement] start date = 2007/05/07 09:22:27 500 JST(org.seasar.teeda.core.lifecycle.impl.InvokeApplicationPhase) DEBUG 2007-05-07 09:22:27,500 [http-8080-Processor24] BEGIN book.chura.web.rentBook.RentBookPage#doSubmit() DEBUG 2007-05-07 09:22:27,500 [http-8080-Processor24] トランザクションを開始しました DEBUG 2007-05-07 09:22:27,500 [http-8080-Processor24] BEGIN book.chura.dao.RentInfoDao#getAllRentInfo(null) DEBUG 2007-05-07 09:22:30,156 [http-8080-Processor24] 物理的なコネクションを取得しました DEBUG 2007-05-07 09:22:30,234 [http-8080-Processor24] 論理的なコネクションを取得しました DEBUG 2007-05-07 09:22:30,796 [http-8080-Processor24] 論理的なコネクションを閉じました DEBUG 2007-05-07 09:22:30,984 [http-8080-Processor24] select user.id, user.name, user.entryDate, user.kind, user.contact, rentInfo.limitDate, bookSpec.title from book left outer join bookSpec on book.bookSpecId = bookSpec.id left outer join rentInfoDetail on book. id = rentInfoDetail.bookId left outer join rentInfo on rentInfoDetail.rentInfoId = rentInfo.id left outer join user on rentInfo.userId = user.id where user.id = null DEBUG 2007-05-07 09:22:30,984 [http-8080-Processor24] 論理的なコネクションを取得しました DEBUG 2007-05-07 09:22:31,031 [http-8080-Processor24] 論理的なコネクションを閉じました DEBUG 2007-05-07 09:22:31,031 [http-8080-Processor24] END book.chura.dao.RentInfoDao#getAllRentInfo(null) : [] DEBUG 2007-05-07 09:22:31,031 [http-8080-Processor24] トランザクションをコミットしました DEBUG 2007-05-07 09:22:31,109 [http-8080-Processor24] END book.chura.web.rentBook.RentBookPage#doSubmit() : null DEBUG 2007-05-07 09:22:31,109 [http-8080-Processor24] [measurement] perform ms:3609 DEBUG 2007-05-07 09:22:31,109 [http-8080-Processor24] [measurement] end date = 2007/05/07 09:22:31 109 JST(org.seasar.teeda.core.lifecycle.impl.InvokeApplicationPhase) DEBUG 2007-05-07 09:22:31,125 [http-8080-Processor24] [measurement] start date = 2007/05/07 09:22:31 125 JST(org.seasar.teeda.core.lifecycle.impl.RenderResponsePhase) DEBUG 2007-05-07 09:22:31,125 [http-8080-Processor24] BEGIN book.chura.web.rentBook.RentBookPage#prerender() DEBUG 2007-05-07 09:22:31,125 [http-8080-Processor24] トランザクションを開始しました DEBUG 2007-05-07 09:22:31,125 [http-8080-Processor24] トランザクションをコミットしました DEBUG 2007-05-07 09:22:31,125 [http-8080-Processor24] END book.chura.web.rentBook.RentBookPage#prerender() : null DEBUG 2007-05-07 09:22:31,484 [http-8080-Processor24] [measurement] perform ms:359 DEBUG 2007-05-07 09:22:31,484 [http-8080-Processor24] [measurement] end date = 2007/05/07 09:22:31 484 JST(org.seasar.teeda.core.lifecycle.impl.RenderResponsePhase) DEBUG 2007-05-07 09:32:31,734 [Thread-33] 物理的なコネクションを閉じました SQLを見ると where user.id = null と nullが設定されているので 文法的には正しいのでしょうか? よくわかりません。 以上、長くなってすみません。 | ||||
|
投稿日時: 2007-05-07 10:19
適当な検索エンジンに、「sql null値」と放り込んでみてください。 お好みのサイトをどうぞ。 | ||||
|
投稿日時: 2007-05-08 15:33
「?」はJDBCのPreparedStatementのプレースホルダーなのではないですか?(推測だけど)
「PreparedStatement」「JDBC」で検索してみてください。 はずしてたらごめんなさい。 | ||||
|
投稿日時: 2007-05-08 15:53
あぁ、なるほど。そういう「?」ですか(推測ですが納得)。 それが「where user.id = null」となってしまっている時点でアウトですね。 http://www.atmarkit.co.jp/bbs/phpBB/viewtopic.php?topic=11422&forum=3&15 | ||||
|
投稿日時: 2007-05-08 19:50
ノラさん、お返事ありがとうございました。
>「?」はJDBCのPreparedStatementのプレースホルダーなのではないですか?(推測だけど) JavaのフレームワークSpringのJDBC抽象フレームワークを参考に書いたのでそういうことになりますね 私も言われてみて、ああそうなのかと思いました。 ありがとうございました。 |