简体   繁体   English

SQL查询返回True / False

[英]SQL query return True/False

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. 该表格完全由数据库驱动,并使用PHP和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. 我正在实现一个新字段,并希望在其中返回一个简单的结果集{True,False}的查询,但是我不想创建一个仅将这两个作为选项的新数据库表,但需要将其保留在框架,因此我需要一个SQL查询来返回结果集,该结果集将用于填充form元素。

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? 是否有某种SQL查询将仅返回结果集{True,False},以便避免制作仅容纳这两个选项的表?

SELECT 'True' UNION SELECT 'False';

SELECT 'true' AS [state] UNION ALL SELECT 'false' 

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? 我猜您正在寻找更复杂的东西吗?

I don't know if this query would work in MySql, but in SQL Server it works. 我不知道此查询是否可以在MySql中使用,但是在SQL Server中可以使用。

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. 您可以针对所需的任何值,任何类型的数据执行此操作。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM