提问者:小点点

MySQL用OR运算符选择


在C#桌面应用程序中,我使用:

"select * from reg where username='" + user + "' and password='" + pass + "' and key='" + key + "'";

选择记录,但我正在试图弄清楚,如果我想从单个输入(如)检查中的两个,如何使用带有条件的运算符。

看起来条件对有效,但在本例中,以下值检查不起作用,似乎我必须使用一些正确的方法:

"select * from reg where username='" + UserOrEmail + "' or email='" + UserOrEmail + "' and password='" + pass + "' and key='" + key + "'";

共1个答案

匿名用户

您需要括号,因为运算符的优先级高于:

"select * from reg where (username='" + UserOrEmail + "' or email='" + UserOrEmail + "') and password='" + pass + "' and key='" + key + "'";

或者使用运算符:

"select * from reg where '" + UserOrEmail + "' in (username, email) and password='" + pass + "' and key='" + key + "'";