quot;where 1=1quot; statement(“其中 1=1陈述)
问题描述
可能的重复:
为什么有人会使用 WHERE 1=1 AND <条件>在 SQL 子句中?
我看到有人用语句查询 MySQL 数据库中的表,如下所示:
I saw some people use a statement to query a table in a MySQL database like the following:
select * from car_table where 1=1 and value="TOYOTA"
但是1=1
在这里是什么意思?
But what does 1=1
mean here?
推荐答案
通常是人们构建 SQL 语句的时候.
It's usually when folks build up SQL statements.
当您添加和value = "Toyota"
时,您不必担心是否有条件之前或只是WHERE.优化器应该忽略它
When you add and value = "Toyota"
you don't have to worry about whether there is a condition before or just WHERE. The optimiser should ignore it
没有魔法,只有实用
示例代码:
commandText = "select * from car_table where 1=1";
if (modelYear <> 0) commandText += " and year="+modelYear
if (manufacturer <> "") commandText += " and value="+QuotedStr(manufacturer)
if (color <> "") commandText += " and color="+QuotedStr(color)
if (california) commandText += " and hasCatalytic=1"
否则你将不得不有一套复杂的逻辑:
Otherwise you would have to have a complicated set of logic:
commandText = "select * from car_table"
whereClause = "";
if (modelYear <> 0)
{
if (whereClause <> "")
whereClause = whereClause + " and ";
commandText += "year="+modelYear;
}
if (manufacturer <> "")
{
if (whereClause <> "")
whereClause = whereClause + " and ";
commandText += "value="+QuotedStr(manufacturer)
}
if (color <> "")
{
if (whereClause <> "")
whereClause = whereClause + " and ";
commandText += "color="+QuotedStr(color)
}
if (california)
{
if (whereClause <> "")
whereClause = whereClause + " and ";
commandText += "hasCatalytic=1"
}
if (whereClause <> "")
commandText = commandText + "WHERE "+whereClause;
这篇关于“其中 1=1"陈述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:“其中 1=1"陈述


- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- SQL 临时表问题 2022-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 更改自动增量起始编号? 2021-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01