I have a table with a lot of columns. Some of these are DATETIME, which I turn into Unix timestamps with UNIX_TIMESTAMP(). So I don't have to type out all the other columns I want from the table, is there a way of doing something like:
SELECT UNIX_TIMESTAMP(t.start) AS start,
       UNIX_TIMESTAMP(t.end) AS end,
       t.theOtherColumns
FROM table t
Where t.theOtherColumns is the rest of the columns in the table. To explain further; I want to select all the columns from the table, perform operations on some of them, but not type out each column name into the query.
When I do, say,
SELECT UNIX_TIMESTAMP(t.start) AS start,
       UNIX_TIMESTAMP(t.end) AS end,
       t.theOtherColumns
FROM table t
It selects start and end twice. I only want to return the start and end columns from UNIX_TIMESTAMP(), and exclude those columns from the t.* set.
What you can do is use this answer to help build the results you want.
A possible solution would look like
SET @sql = CONCAT('SELECT UNIX_TIMESTAMP(t.start) AS start, UNIX_TIMESTAMP(t.end) as end,', (SELECT GROUP_CONCAT(COLUMN_NAME) FROM 
information_schema.columns WHERE table_schema = 'test' AND table_name = 
't' AND column_name NOT IN ('start', 'end')), 
' from test.t');  
PREPARE stmt1 FROM @sql;
EXECUTE stmt1;
*Replace test with the name of the schema that contains your table t.
Try t.* it works under Oracle.
I don't believe there is a way to do this as you suggested, but you can do this
SELECT t.*, UNIX_TIMESTAMP(t.start) AS start, UNIX_TIMESTAMP(t.end) as end ...
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With