tags:

views:

611

answers:

5

I am working with an existing architecture which populates fields on a form with things from the database. The form is entirely database driven and is done in PHP and MySQL.

The problem is that the database table that holds the form elements has a field for the query that will return a result set that will be used to populate the select dropdown form element. This works great for fields like States, countries, and other such fields which are particular to our organization.

I am implementing a new field and want to put a query in that returns a simple resultset {True, False} but I don't want to create a new database table that simply has those two as options, but need to keep it within the framework, so I need a SQL query to return the resultset which will be used to populate the form element.

Is there some kind of a SQL query that will just return the resultset {True, False} so that I can avoid making a table just to house those two options?

+1  A: 

SELECT 'True' UNION SELECT 'False';

hrnt
This is exactly what I was looking for. Didn't think of doing it like that, thanks!
vaporstun
Why not UNION ALL? Why no "column" name?
gbn
@hrnt: UNION removes dupes, UNION ALL preserves. No column name is bad practice, but hey...
gbn
Yeah, I mixed up UNION ALL and UNION (deleted the offending comment) :( Anyways, that is the simplest query that works.
hrnt
UNION ALL might be preferable because the dbms might want to sort the result set to ensure uniqueness (yes it's bad to rely on sort order without ORDER BY, but in this case it probably works)
erikkallen
+1  A: 
SELECT 'true' AS [state] UNION ALL SELECT 'false'
gbn
+1  A: 

You mean something like this?:

select 'True' as value
union all
select 'False' as value

I'm guessing there is something more complex you are looking for?

chadhoc
A: 

You could serialize an array for storage in the database as a string and unserialize it on the way out.

$array = array(true, false);

Going into the db:

serialize($array);

Coming out of the db:

unserialize(YOUR_DB_RESULT_HERE);
cballou
A: 

I don't know if this query would work in MySql, but in SQL Server it works.

You can do that query:

SELECT 'True'
UNION
SELECT 'False'

You don't have to specify a table. You can do that for any value that you need, for any type of data.

Danielle