【问题标题】:how to insert value in mysql only if it already doesn't exist?仅当它已经不存在时如何在mysql中插入值?
【发布时间】:2015-04-08 08:02:31
【问题描述】:

我已经在java中编写了这个sql查询,如果它不存在则在数据库中插入记录以避免重复条目......

    PreparedStatement pst = connection.prepareStatement("INSERT INTO tbl_review (?,?,?,?,?,?,?) SELECT t1.product_name,t1.product_model_no,t1.user_username,t1.user_contact FROM tbl_review t1 WHERE NOT EXISTS (SELECT t2.product_name,t2.product_model_no,t2.user_username,t2.user_contact FROM tbl_review t2 WHERE t2.product_name = t1.product_name AND t2.product_model_no = t1.product_model_no AND t2.user_username = t1.user_username AND t2.user_contact = t1.user_contact)");

                    pst.setString(1,null);  //This field is autoincrement
                    pst.setString(2,prodname);
                    pst.setString(3,prodmodel);
                    pst.setString(4,user);
                    pst.setLong(5,usercon);
                    pst.setString(6,review);
                    pst.setString(7,rating);
int numRowsChanged = pst.executeUpdate();

我用过这个查询...这个查询不起作用..我不知道它是否正确..

    INSERT INTO tbl_review (?,?,?,?,?,?,?) 
SELECT t1.product_name,t1.product_model_no,t1.user_username,t1.user_contact FROM tbl_review t1 
WHERE NOT EXISTS 
(SELECT t2.product_name,t2.product_model_no,
t2.user_username,t2.user_contact 
FROM tbl_review t2 
WHERE t2.product_name = t1.product_name AND 
t2.product_model_no = t1.product_model_no AND 
t2.user_username = t1.user_username AND 
t2.user_contact = t1.user_contact)

我收到了这个错误

INFO: Server startup in 1027 ms
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'null,'Some Product','S657','username',9944558675,'sdfdsfdf','3') SELECT t1.pro' at line 1
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.Util.getInstance(Util.java:386)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3609)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3541)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2002)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2163)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2624)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2127)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2427)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2345)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2330)
    at com.stc.AddReview.doPost(AddReview.java:79)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)

【问题讨论】:

  • 从 ?列表到列名列表。 (或者在测试时将其删除。)

标签: java mysql sql jdbc


【解决方案1】:

使用INSERT IGNORE INTO table

这不会在 mySql 表中插入重复的行。

【讨论】:

  • INSERT IGNORE 会起作用如果我删除了第一列的自动增量.....但我想保留它并检查具有相同条目的其他列......
【解决方案2】:

您可以选择该行。如果没有结果,请插入。

SELECT t1.product_name,t1.product_model_no,t1.user_username,t1.user_contact FROM tbl_review t1 WHERE t1.product_name = :paramName AND t1.product_model_no = :paramModNo AND ...

【讨论】:

  • 我想到了这个...我可以编写两个不同的查询,并且可以按照您所说的进行插入,但是如果它在单个查询中完成,那将更合适...。 .
【解决方案3】:

insert Stmt 包含自动增量字段吗?您尝试使用

进行设置
pst.setString(1,null);  //This field is autoincrement

由于该字段是 AutoIncrement,因此不应在插入语句中使用它。 你也可以提高你的存在以提高性能

SELECT t1.product_name,t1.product_model_no,t1.user_username,t1.user_contact FROM tbl_review t1 
WHERE NOT EXISTS 
(SELECT 1
FROM tbl_review t2 
WHERE t2.product_name = t1.product_name AND 
t2.product_model_no = t1.product_model_no AND 
t2.user_username = t1.user_username AND 
t2.user_contact = t1.user_contact)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-28
    • 1970-01-01
    • 2018-01-17
    • 2012-05-21
    • 2018-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多