【问题标题】:How to write OR condition in Select Query如何在 Select Query 中写入 OR 条件
【发布时间】:2018-04-30 08:14:46
【问题描述】:

mysql 有没有办法表达 'OR' ?这个想法是搜索是否存在与username 列或email 列对应的匹配"user.getinput()"。如何以适当的方式表达这一点。

SELECT  FROM tablex WHERE username OR email = '"+ user.getinput()+"'";

【问题讨论】:

  • username = ? OR email = ? 并使用带有适当 SQL 参数的准备好的语句

标签: mysql select where


【解决方案1】:

以下是查询:

SELECT  FROM tablex WHERE username = ? OR email = ?

更好地使用PreparedStatement

String query = "SELECT  FROM tablex WHERE username = ? OR email = ?";
PreparedStatement stmt=con.prepareStatement(query);  
stmt.setString(1,userName);
stmt.setString(2,email);  

stmt.executeUpdate();

【讨论】:

  • 我的错!更新一样
【解决方案2】:

我强烈建议使用准备好的语句来防止 sql 注入问题。

类似这样的:

PreparedStatement stmt = conn.prepareStatement(
     "SELECT * FROM tablex WHERE username = ? OR email = ?");
stmt.setParameter(1, "something");
stmt.setParamater(2, "something");

【讨论】:

    猜你喜欢
    • 2017-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-08
    • 1970-01-01
    • 1970-01-01
    • 2020-12-07
    • 2021-03-02
    相关资源
    最近更新 更多