Is it possible to select a table column without entering its name in SQL?

Asked

Viewed 200 times

1

So I was researching but I couldn’t find anything about it, but I need to sort of select the first and second column of the table, without knowing what they are. My code is this:

<?php $preparaNormalizacao=$con->prepare("SELECT * FROM ".$tabelaExterna." INNER JOIN ".$tabelaNormalizacao." ON ".$tabelaNormalizacao.".".$cTEnormaliza."=".$tabelaExterna.".".$cTEexterna);?>

I need to do something like this here, where I don’t know the names Coluna1 and Coluna2:

<?php $preparaNormalizacao=$con->prepare("SELECT Coluna1,Coluna2 FROM ".$tabelaExterna." INNER JOIN ".$tabelaNormalizacao." ON ".$tabelaNormalizacao.".".$cTEnormaliza."=".$tabelaExterna.".".$cTEexterna);?>

Is it possible? Thanks in advance!

1 answer

1


You can do it like this:

1) I first built a dynamic query

SELECT 
   CONCAT('SELECT ', GROUP_CONCAT(COLUMN_NAME), ' FROM test') 
FROM 
  (
    SELECT COLUMN_NAME, ORDINAL_POSITION 
    FROM INFORMATION_SCHEMA.COLUMNS 
    WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'test' 
    ORDER BY ORDINAL_POSITION DESC 
    LIMIT 10
) AS ord_desc 
ORDER BY ord_desc.ORDINAL_POSITION

The result will be something like this:

SELECT date,title FROM test

Then with the result in php opens a new connection of the previous SQL

  • Opa Valeu, in case I just needed to adjust the LIMIT to start and finish in the position I want.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.