pg.ddx.io pgsql-jdbc@postgresql.org mailing list archive
help / color / mirror / Atom feedPreparedStatement vs. Statement problem
4+ messages / 3 participants
[nested] [flat]
* PreparedStatement vs. Statement problem
@ 2007-11-29 06:37 yoursoft <yoursoft@freemail.hu>
0 siblings, 1 reply; 4+ messages in thread
From: yoursoft @ 2007-11-29 06:37 UTC (permalink / raw)
To: pgsql-jdbc
I have a problem:
SELECT * FROM mytable WHERE c1 like 'a' UNION ALL SELECT * FROM mytable
WHERE c2 like 'a' || '%' AND c2 not like 'a' UNION ALL SELECT * FROM
mytable WHERE c2 like '%' || 'a' || '%' AND c2 not like 'a' || '%' AND
c2 not like 'a' LIMIT 101 OFFSET 0
Where 'a' is a variable.
When I run this query from Statement. There is no problem. The result is
ordered by: First SELECT, second SELECT third SELECT.
When I run this query from PreparedStatement ('a' values replaced by ?).
The result is ordered by in alphabets.Why?
Regards,
Ferenc
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: PreparedStatement vs. Statement problem
@ 2007-11-29 09:21 Oliver Jowett <oliver@opencloud.com>
parent: yoursoft <yoursoft@freemail.hu>
0 siblings, 1 reply; 4+ messages in thread
From: Oliver Jowett @ 2007-11-29 09:21 UTC (permalink / raw)
To: yoursoft <yoursoft@freemail.hu>; +Cc: pgsql-jdbc
yoursoft wrote:
> I have a problem:
> SELECT * FROM mytable WHERE c1 like 'a' UNION ALL SELECT * FROM mytable
> WHERE c2 like 'a' || '%' AND c2 not like 'a' UNION ALL SELECT * FROM
> mytable WHERE c2 like '%' || 'a' || '%' AND c2 not like 'a' || '%' AND
> c2 not like 'a' LIMIT 101 OFFSET 0
>
> Where 'a' is a variable.
> When I run this query from Statement. There is no problem. The result is
> ordered by: First SELECT, second SELECT third SELECT.
> When I run this query from PreparedStatement ('a' values replaced by ?).
> The result is ordered by in alphabets.Why?
You don't have an ORDER BY so the result ordering is undefined. The
planner happens to pick different plans in the two cases which result in
different orderings.
-O
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: PreparedStatement vs. Statement problem
@ 2007-11-29 09:39 Oliver Jowett <oliver@opencloud.com>
parent: Oliver Jowett <oliver@opencloud.com>
0 siblings, 1 reply; 4+ messages in thread
From: Oliver Jowett @ 2007-11-29 09:39 UTC (permalink / raw)
To: yoursoft <yoursoft@freemail.hu>; +Cc: pgsql-jdbc
yoursoft wrote:
> Ok, the planner uses different way. I think the planner use the UNION in
> PreparedStatement and not the UNION ALL?
> How to rewrite the SQL to use the UNION ALL in preparedstatement?
You need an ORDER BY or the order is undefined and unpredictable. See
http://www.postgresql.org/docs/8.2/static/queries-order.html
There's no simple way to say "give me rows from this table first" other
than running a query on each table separately.
-O
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: PreparedStatement vs. Statement problem
@ 2007-11-29 10:18 tivvpgsqljdbc@gtech-ua.com
parent: Oliver Jowett <oliver@opencloud.com>
0 siblings, 0 replies; 4+ messages in thread
From: tivvpgsqljdbc@gtech-ua.com @ 2007-11-29 10:18 UTC (permalink / raw)
To: Oliver Jowett <oliver@opencloud.com>; +Cc: yoursoft <yoursoft@freemail.hu>; pgsql-jdbc
Oliver Jowett wrote:
> yoursoft wrote:
>> Ok, the planner uses different way. I think the planner use the UNION
>> in PreparedStatement and not the UNION ALL?
>> How to rewrite the SQL to use the UNION ALL in preparedstatement?
>
> You need an ORDER BY or the order is undefined and unpredictable. See
> http://www.postgresql.org/docs/8.2/static/queries-order.html
>
> There's no simple way to say "give me rows from this table first"
> other than running a query on each table separately.
Actually the simple way is
select c1,c2,c3,...,cn from (select 1, table1.* from table1 union all
select 2, table2.* from table2 union all ...) a(ordercolumn, c1, c2, c3,
.., cn) order by 1
^ permalink raw reply [nested|flat] 4+ messages in thread
end of thread, other threads:[~2007-11-29 10:18 UTC | newest]
Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2007-11-29 06:37 PreparedStatement vs. Statement problem yoursoft <yoursoft@freemail.hu>
2007-11-29 09:21 ` Oliver Jowett <oliver@opencloud.com>
2007-11-29 09:39 ` Oliver Jowett <oliver@opencloud.com>
2007-11-29 10:18 ` tivvpgsqljdbc@gtech-ua.com
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox