WHERE 1在MySQL查询中的重要性 [英] Importance of WHERE 1 in MySQL queries

查看:64
本文介绍了WHERE 1在MySQL查询中的重要性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

进行没有WHERE约束的MySQL查询时,大多数人在查询中使用WHERE 1.但是,省略WHERE 1不会影响查询.两者之间有区别吗?被认为是最佳实践吗?

When making a MySQL query with no WHERE constraints, most people use WHERE 1 in the query. However, omitting WHERE 1 does not influence the query. Is there a difference between the two? Is one considered to be the best practice?

推荐答案

我认为这不是最佳实践,但是人们有时会使用它来简化动态查询的构建.

I don't think it's a matter of best practice, but people sometimes use it to make building dynamic queries a bit easier.

string sql = "SELECT * FROM mytable WHERE 1 ";
if ( somecondition ) {
   sql += "AND somefield = somevalue ";
}

if ( someothercondition ) {
   sql += "AND someotherfield = someothervalue ";
}

... etc

如果没有WHERE 1,我需要检查每个if块是否需要放入WHEREAND.

Without the WHERE 1 in there I would need to check in each if block whether I needed to put in a WHERE or an AND.

这篇关于WHERE 1在MySQL查询中的重要性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆