Thread: SELECT with WHERE clause by column number

SELECT with WHERE clause by column number

From
Carlos Santos
Date:
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


Yahoo! Acesso Grátis - Internet rápida e grátis. Instale o discador agora!

Re: SELECT with WHERE clause by column number

From
Richard Huxton
Date:
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


Re: SELECT with WHERE clause by column number

From
"Aaron Bono"
Date:
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
==================================================================

Re: SELECT with WHERE clause by column number

From
Ragnar
Date:
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