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

1

So I was searching but I did not find anything about it, but I need to sort the first and second columns 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 do not know the names Column1 and Column2:

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

Is it possible? Thank you in advance!

    
asked by anonymous 19.06.2016 / 00:18

1 answer

1

You can do this:

1) First build 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 look something like this:

SELECT date,title FROM test

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

    
19.06.2016 / 00:29