user5013
user5013

Reputation: 1021

How to append a value to a MySQL query

I guess this is a bit of a noob question but I haven't seen this question asked so I don't know if it is possible. I would like to construct a MySQL query where I can append a value to the result. What I mean by way of an example:

SELECT node.title AS node_title
FROM program p
INNER JOIN node n ON p.nid = n.nid ORDER BY node_title

So this query will return a result and now I would like to either append or prepend an arbitrary value to the result to get the following:

arbitrary_value
result1
result2
result3
etc...

So is there some MySQL statement that wraps this query to achieve this effect?

Upvotes: 0

Views: 291

Answers (2)

Mike Dinescu
Mike Dinescu

Reputation: 55760

Yes, you can accomplish that using the UNION operator:

SELECT 'arbitrary_value' as node_title
UNION ALL
(SELECT n.title AS node_title
  FROM program p
  INNER JOIN node n ON p.nid = n.nid ORDER BY node_title)

Upvotes: 3

Stelian Matei
Stelian Matei

Reputation: 11633

You could use UNION syntax

SELECT arbitrary_value AS node_title

UNION ALL

SELECT node.title
FROM program p
INNER JOIN node n ON p.nid = n.nid 

ORDER BY node_title

Please note that the column alias has to go in the first SELECT.

Upvotes: 0

Related Questions