Select 并在 SQL 语句中设置值
Select and Set Value in SQL Statement
我有一个相当古怪的问题。我可以在 SELECT
语句中设置默认值吗?
在下面的查询中,我希望 boolItem
始终为假(不从数据库中检索)。我知道这很疯狂,但请耐心等待我解释原因。
SELECT id, boolItem = False
FROM MyTable;
我正在使用现有的大型 SQL 数据库和项目。我正在查询数据并将它们作为 Action
C# 对象返回。操作可以由用户自定义或标准操作。这由 属性 IsCustom
.
表示
public class Action
{
public int Id { get; set; }
public bool IsCustom { get; set; }
.....
}
在 SQL 数据库中,自定义操作存储在 table custom_actions
中,标准操作存储在 table actions
.[=31= 中]
我使用以下代码检索并存储 Action
对象。我想让 actions
table 的查询始终将 属性 IsCustom
设置为 false。 custom_actions
table 的查询总是将 属性 IsCustom
设置为 true。我正在使用查询 SELECT a.id AS Id, a.is_custom = false AS IsCustom
,它不是有效代码,因为 table 没有 is_custom
列,但它是为了演示我正在尝试做的事情。
public async Task<IEnumerable<Models.Action>> ExecuteAsync (IDbConnection conn, IDbTransaction transition, long userId)
{
string sql = @"SELECT a.id AS Id, a.is_custom = false AS IsCustom
FROM actions a
INNER JOIN members_actions ma ON a.id = ma.action_id AND is_custom = false
WHERE ma.member_id = :userId
UNION
SELECT a.id AS Id, a.is_custom = true AS IsCustom
FROM custom_actions a
INNER JOIN members_actions ma ON a.id = ma.action_id AND is_custom = true
WHERE ma.member_id = :userId;";
return await conn.QueryAsync<Models.Action> (sql, new {userId = userId}, transition);
}
Table 'Actions' 列数 = id || description || name
Table 'Custom_actions' 列 = id || description || name || parameters
这可能吗?它比在结构上更改数据库(将 2 table 合并为 1 并添加一个 is_custom
列)更好。
您可以只 select 值 true
或 false
并使用别名来指定列名 IsCustom
例如,我在下面修改了您的示例以说明如何执行此操作(并且还从 JOIN 条件中删除了 AND is_custom = false/true
,因为它似乎没有 is_custom
列table)。
public async Task<IEnumerable<Models.Action>> ExecuteAsync (IDbConnection conn, IDbTransaction transition, long userId)
{
string sql = @"SELECT a.id AS Id, false AS IsCustom
FROM actions a
INNER JOIN members_actions ma ON a.id = ma.action_id
WHERE ma.member_id = :userId
UNION
SELECT a.id AS Id, true AS IsCustom
FROM custom_actions a
INNER JOIN members_actions ma ON a.id = ma.action_id
WHERE ma.member_id = :userId;";
return await conn.QueryAsync<Models.Action> (sql, new {userId = userId}, transition);
}
我有一个相当古怪的问题。我可以在 SELECT
语句中设置默认值吗?
在下面的查询中,我希望 boolItem
始终为假(不从数据库中检索)。我知道这很疯狂,但请耐心等待我解释原因。
SELECT id, boolItem = False
FROM MyTable;
我正在使用现有的大型 SQL 数据库和项目。我正在查询数据并将它们作为 Action
C# 对象返回。操作可以由用户自定义或标准操作。这由 属性 IsCustom
.
public class Action
{
public int Id { get; set; }
public bool IsCustom { get; set; }
.....
}
在 SQL 数据库中,自定义操作存储在 table custom_actions
中,标准操作存储在 table actions
.[=31= 中]
我使用以下代码检索并存储 Action
对象。我想让 actions
table 的查询始终将 属性 IsCustom
设置为 false。 custom_actions
table 的查询总是将 属性 IsCustom
设置为 true。我正在使用查询 SELECT a.id AS Id, a.is_custom = false AS IsCustom
,它不是有效代码,因为 table 没有 is_custom
列,但它是为了演示我正在尝试做的事情。
public async Task<IEnumerable<Models.Action>> ExecuteAsync (IDbConnection conn, IDbTransaction transition, long userId)
{
string sql = @"SELECT a.id AS Id, a.is_custom = false AS IsCustom
FROM actions a
INNER JOIN members_actions ma ON a.id = ma.action_id AND is_custom = false
WHERE ma.member_id = :userId
UNION
SELECT a.id AS Id, a.is_custom = true AS IsCustom
FROM custom_actions a
INNER JOIN members_actions ma ON a.id = ma.action_id AND is_custom = true
WHERE ma.member_id = :userId;";
return await conn.QueryAsync<Models.Action> (sql, new {userId = userId}, transition);
}
Table 'Actions' 列数 = id || description || name
Table 'Custom_actions' 列 = id || description || name || parameters
这可能吗?它比在结构上更改数据库(将 2 table 合并为 1 并添加一个 is_custom
列)更好。
您可以只 select 值 true
或 false
并使用别名来指定列名 IsCustom
例如,我在下面修改了您的示例以说明如何执行此操作(并且还从 JOIN 条件中删除了 AND is_custom = false/true
,因为它似乎没有 is_custom
列table)。
public async Task<IEnumerable<Models.Action>> ExecuteAsync (IDbConnection conn, IDbTransaction transition, long userId)
{
string sql = @"SELECT a.id AS Id, false AS IsCustom
FROM actions a
INNER JOIN members_actions ma ON a.id = ma.action_id
WHERE ma.member_id = :userId
UNION
SELECT a.id AS Id, true AS IsCustom
FROM custom_actions a
INNER JOIN members_actions ma ON a.id = ma.action_id
WHERE ma.member_id = :userId;";
return await conn.QueryAsync<Models.Action> (sql, new {userId = userId}, transition);
}