このディレクトリの索引
http://hibari.2ch.net/test/read.cgi/db/1299305530/76
#  副問い合わせ?を一回ですませたい 
#  OracleですがMySQLでもいいです 
#   
#  SELECT * FROM hoge  
#  WHERE fuga < (SELECT fuga+5 FROM hoge WHERE id=1 ) 
#    AND fuga > (SELECT fuga+10 FROM hoge WHERE id=1 ) 
#   
#  ↑をBETWEENにすると 
#   
#  SELECT * FROM hoge  
#  WHERE fuga BETWEEN ( 
#   SELECT fuga+5 FROM hoge WHERE id=1 
#  ) and ( 
#   SELECT fuga+10 FROM hoge WHERE id=1 
#  ) 
#   
#  となると思うのですが、これを 
#   
#  SELECT * FROM hoge  
#  WHERE fuga BETWEEN ( 
#   SELECT fuga+5, fuga+10 FROM hoge WHERE id=1 
#  ) 
#   
#  みたいに簡単に書けないでしょうか。 
#  上ふたつの書き方だと2回副問い合わせが走ってそうでおそくなる気がします。 
#   

'SELECT * FROM hoge WHERE fuga < (SELECT fuga+5 FROM hoge WHERE id=1 ) AND fuga > (SELECT fuga+10 FROM hoge WHERE id=1 )'(_id,_fuga) :-
        hoge(1,_fuga_1),
        hoge(_id,_fuga),
        _fuga > _fuga_1 + 5,
        _fuga < _fuga_1 + 10.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

テーブル構造(hoge,1,id).
テーブル構造(hoge,2,fuga).

項目位置(hoge,id,[_id,_],_id).
項目位置(hoge,fuga,[_,_fuga],_fuga).

'SELECT * FROM hoge WHERE fuga < (SELECT fuga+5 FROM hoge WHERE id=1 ) AND fuga > (SELECT fuga+10 FROM hoge WHERE id=1 ) の 最も丁寧な解は'(_id,_fuga) :-
         findall(_,テーブル構造(hoge,_,_),L1),
         P1 =.. [hoge|L1],
         項目位置(hoge,id,L1,1),
         項目位置(hoge,fuga,L1,_fuga_1),
         call(P1),
         findall(_,テーブル構造(hoge,_,_),L2),
         P2 =.. [hoge|L2],
         項目位置(hoge,id,L2,_id),
         項目位置(hoge,fuga,L2,_fuga),
         call(P2),
         _fuga > _fuga_1 + 5,
         _fuga < _fuga_1 + 10.