Koray Tugay
Koray Tugay

Reputation: 23800

Is there a way to add header to mysql query results?

For example:

mysql> SELECT billing_amount, billing_currency_code, source_amount, source_currency FROM visa_record;
+----------------+-----------------------+---------------+-----------------+
| billing_amount | billing_currency_code | source_amount | source_currency |
+----------------+-----------------------+---------------+-----------------+
|          31651 | EUR                   |         48563 | USD             |
+----------------+-----------------------+---------------+-----------------+

But what I want is something like:

Visa Record Information
+----------------+-----------------------+---------------+-----------------+
| billing_amount | billing_currency_code | source_amount | source_currency |
+----------------+-----------------------+---------------+-----------------+
|          31651 | EUR                   |         48563 | USD             |
+----------------+-----------------------+---------------+-----------------+

Upvotes: 0

Views: 2299

Answers (2)

Matt
Matt

Reputation: 15071

This is not what SQL (or any DBMS should be used for).

However if you have no other alternative use UNION ALL

SELECT 'billing_amount'AS'Visa Record Information', 'billing_currency_code'AS'', 'source_amount'AS'','source_currency 'AS''
FROM visa_record
UNION ALL
SELECT billing_amount, billing_currency_code, source_amount, source_currency 
FROM visa_record

Upvotes: 1

Shadow
Shadow

Reputation: 34232

Matt is right with the union all and also with the statement that this is not what the command line client is meant for.

However, Matt's answer is incomplete:

SELECT 'Visa Record Information', null, null, null
FROM visa_record LIMIT 1
UNION ALL
SELECT 'billing_amount', 'billing_currency_code', 'source_amount', 'source_currency'
FROM visa_record LIMIT 1
UNION ALL
SELECT billing_amount, billing_currency_code, source_amount, source_currency 
FROM visa_record 

Pls note, that this approach will not work on some earlier (pre v5.0) mysql versions, since those versions used the data type and length from the 1st query to determine the data type and length for all queries. You may want to use '' (empty string) instead of null in the 1st query.

Upvotes: 3

Related Questions