MySQL检查某列是否已存在。

huangapple go评论79阅读模式
英文:

MySQL checking if something is already in a column

问题

以下是翻译好的内容:

我对MySQL还不太了解,不太清楚如何检查在“friendCode”列中是否已经存在相同的值,如果是这样的话,还需要检查在同一行的“watched”列中是否存在值“0”。我已经成功地检查了代码是否存在于某一行中。我只是不知道如何查找第二个条件。

这是表格的外观:

MySQL检查某列是否已存在。

这是我已经完成的部分:

public static boolean checkRow(String query) {

    boolean ret = false;
    try (Connection con = DriverManager.getConnection(url, user, password)) {

        Statement stmt = con.createStatement();
        stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery(query);

        if (rs.next() && !rs.getString("watched").equals(0)) {
            ret = false;
        } else {
            ret = true;
        }
        rs.close();
        stmt.close();
    } catch (SQLException e) {
        System.out.println(e.getMessage());
    }
    return ret;
}
英文:

I am new to MySQL and don't really know how to check if in the column "friendCode" the same value already exist and if that's the case it should look if in the same row in the "watched" column the value "0" is. I have already managed to check whether the code exists in a row. I just don't know how to look for the second condition.

Here is a picture how the table looks like:

MySQL检查某列是否已存在。

And here that what I already managed to do:

	public static boolean checkRow(String query) {

		boolean ret = false;
		try (Connection con = DriverManager.getConnection(url, user, password)) {

			Statement stmt = con.createStatement();
			stmt = con.createStatement();
			ResultSet rs = stmt.executeQuery(query);

			if (rs.next() && !rs.getString("watched").equals(0)) {
				ret = false;
			} else {
				ret = true;
			}
			rs.close();
			stmt.close();
		} catch (SQLException e) {
			System.out.println(e.getMessage());
		}
		return ret;
}

答案1

得分: 1

  1. 我认为你在这个程序中没有编写任何选择查询。如果你是从主方法调用的,只需检查并尝试以下步骤。

  2. 在给定的表中,"watched" 列具有整数数据类型。

  3. 你在进行字符串类型的比较。

  4. 试试我的代码,它能正常工作。如果你有任何疑虑,请回复这条评论。

public static boolean checkRow(String query) {
    boolean ret = false;
    try (Connection con = DriverManager.getConnection(url, user, password)) {

        Statement stmt = con.createStatement();
        stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery(query);

        if (rs.next() && rs.getString("watched")!=0) {
            ret = false;
        } else {
            ret = true;
        }
        rs.close();
        stmt.close();
    } catch (SQLException e) {
        System.out.println(e.getMessage());
    }
    return ret;
}
英文:
  1. I think you are not written any select query in this program. If you are calling from the main method just check and try the below steps.

  2. In Given Table watched column have integer DataType data.

  3. Your Compare to String type.

  4. Try it my code it's working if you have any concern revert back to this comment.

    public static boolean checkRow(String query) {
         boolean ret = false;
         try (Connection con = DriverManager.getConnection(url, user, password)) {
    
             Statement stmt = con.createStatement();
             stmt = con.createStatement();
             ResultSet rs = stmt.executeQuery(query);
    
             if (rs.next() && rs.getString("watched")!=0) {
                 ret = false;
             } else {
                 ret = true;
             }
             rs.close();
             stmt.close();
         } catch (SQLException e) {
             System.out.println(e.getMessage());
         }
         return ret;
    }
    

huangapple
  • 本文由 发表于 2020年8月27日 17:22:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/63612975.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定