pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
SELECT with WHERE clause by column number
4+ messages / 4 participants
[nested] [flat]

* SELECT with WHERE clause by column number
@ 2006-12-18 17:17 Carlos Santos <carloscompels@yahoo.com.br>
  2006-12-18 17:25 ` Re: SELECT with WHERE clause by column number Richard Huxton <dev@archonet.com>
  2006-12-18 17:36 ` Re: SELECT with WHERE clause by column number Aaron Bono <postgresql@aranya.com>
  2006-12-18 21:56 ` Re: SELECT with WHERE clause by column number Ragnar <gnari@hive.is>
  0 siblings, 3 replies; 4+ messages in thread

From: Carlos Santos @ 2006-12-18 17:17 UTC (permalink / raw)
  To: pgsql-sql

Hi!

I need to make a query like this:

SELECT id FROM myTable WHERE column-number = 'value';
(PS: The id column is the primary key of myTable).

That is a select using column number in the WHERE clause what don't exists in SQL.

I need this because there's a situation in my program where I don't have the column name.
I've solved that querying Postgresql the name of the column with that number, and then creating  the SELECT query.
But this solution is slow... two database calls...

Can I do this with a single query or in a faster way  through SQL, an internal function or through a Procedural Language?

Thanks 
 
 Carlos Henrique Iazzetti Santos 

Compels Informática 
 Santa Rita do Sapucaí - MG
BRAZIL
www.compels.net






	




	
		
_______________________________________________________ 
Você quer respostas para suas perguntas? Ou você sabe muito e quer compartilhar seu conhecimento? Experimente o Yahoo! Respostas !
http://br.answers.yahoo.com/

^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: SELECT with WHERE clause by column number
  2006-12-18 17:17 SELECT with WHERE clause by column number Carlos Santos <carloscompels@yahoo.com.br>
@ 2006-12-18 17:25 ` Richard Huxton <dev@archonet.com>
  2 siblings, 0 replies; 4+ messages in thread

From: Richard Huxton @ 2006-12-18 17:25 UTC (permalink / raw)
  To: Carlos Santos <carlos@compels.net>; +Cc: pgsql-sql

Carlos Santos wrote:
> SELECT id FROM myTable WHERE column-number = 'value';
> (PS: The id column is the primary key of myTable).
> 
> That is a select using column number in the WHERE clause what don't exists in SQL.
> 
> I need this because there's a situation in my program where I don't have the column name.
> I've solved that querying Postgresql the name of the column with that number, and then creating  the SELECT query.
> But this solution is slow... two database calls...

If you don't know what the column is, how do you know what you are 
testing against? Or what type it is, for that matter?

Anyway, just have build a list of column-name,column-type pairs for 
relevant tables at application start-up, or store it in a configuration 
file. Unless you're building/changing tables all the time, that should work.

I'm curious as to what type of application can usefuly query a database 
without knowing what structure it has.

-- 
   Richard Huxton
   Archonet Ltd



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: SELECT with WHERE clause by column number
  2006-12-18 17:17 SELECT with WHERE clause by column number Carlos Santos <carloscompels@yahoo.com.br>
@ 2006-12-18 17:36 ` Aaron Bono <postgresql@aranya.com>
  2 siblings, 0 replies; 4+ messages in thread

From: Aaron Bono @ 2006-12-18 17:36 UTC (permalink / raw)
  To: Carlos Santos <carlos@compels.net>; +Cc: pgsql-sql

On 12/18/06, Carlos Santos <carloscompels@yahoo.com.br> wrote:
>
> Hi!
>
> I need to make a query like this:
>
> SELECT id FROM myTable WHERE column-number = 'value';
> (PS: The id column is the primary key of myTable).
>
> That is a select using column number in the WHERE clause what don't exists
> in SQL.
>
> I need this because there's a situation in my program where I don't have
> the column name.
> I've solved that querying Postgresql the name of the column with that
> number, and then creating  the SELECT query.
> But this solution is slow... two database calls...
>
> Can I do this with a single query or in a faster way through SQL, an
> internal function or through a Procedural Language?
>

Can you provide the 2 queries you used to successfully do this in two
database calls?

I am not sure that select makes sense.  Where clauses work on rows, not
columns.  Having your two working queries will help us see what you are
really trying to do.

-- 
==================================================================
   Aaron Bono
   Aranya Software Technologies, Inc.
   http://www.aranya.com
   http://codeelixir.com
==================================================================

^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: SELECT with WHERE clause by column number
  2006-12-18 17:17 SELECT with WHERE clause by column number Carlos Santos <carloscompels@yahoo.com.br>
@ 2006-12-18 21:56 ` Ragnar <gnari@hive.is>
  2 siblings, 0 replies; 4+ messages in thread

From: Ragnar @ 2006-12-18 21:56 UTC (permalink / raw)
  To: Carlos Santos <carlos@compels.net>; +Cc: pgsql-sql

On mán, 2006-12-18 at 09:17 -0800, Carlos Santos wrote:

> 
> SELECT id FROM myTable WHERE column-number = 'value';
> (PS: The id column is the primary key of myTable).
> 
> That is a select using column number in the WHERE clause what don't
> exists in SQL.
> 
> I need this because there's a situation in my program where I don't
> have the column name.
> I've solved that querying Postgresql the name of the column with that
> number, and then creating  the SELECT query.
> But this solution is slow... two database calls...
> 
> Can I do this with a single query or in a faster way through SQL, an
> internal function or through a Procedural Language?

this can be done with the more dynamic prodedural languages.

this should be possible too with pl/pgsql using your first query
+ EXECUTE
this is still 2 queries, but only one client round-trip.

this can also be done in one SQL query with some creative
misuse of an UNION ALL within a subquery, if you know the 
number of columns in your table.

gnari







^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2006-12-18 21:56 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2006-12-18 17:17 SELECT with WHERE clause by column number Carlos Santos <carloscompels@yahoo.com.br>
2006-12-18 17:25 ` Richard Huxton <dev@archonet.com>
2006-12-18 17:36 ` Aaron Bono <postgresql@aranya.com>
2006-12-18 21:56 ` Ragnar <gnari@hive.is>

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