0

好的,我希望在 mySQL 中针对 Wordpress 数据库运行循环查询。某些记录具有 [城市名称] 的自定义字段(例如:meta_key = 'sidebar' & meta_value = 'chicago')。问题是有些人有这个自定义字段,而有些人没有。我已经通过以下查询隔离了未通过以下查询的记录:

SELECT object_id 
FROM wp_term_relationships 
WHERE term_taxonomy_id = (
    SELECT term_id FROM wp_terms 
    WHERE name = 'Chicago') 
  AND object_id > 5911

生成的 object_id 集将需要具有

INSERT INTO wp_postmeta (meta_key, meta_value,post_id) 
VALUES ('sidebar','chicago',[object_id])

所以我的问题是如何组合这些,以便 SELECT 循环运行 INSERT 为结果的每个 object_id 。任何想法将不胜感激。谢谢-

4

2 回答 2

2

你可以试试

INSERT INTO wp_postmeta (meta_key, meta_value,post_id)
SELECT 'sidebar','chicago', object_id 
  FROM wp_term_relationships 
 WHERE term_taxonomy_id = 
       (
          SELECT term_id 
            FROM wp_terms 
           WHERE name = 'Chicago'
       ) 
   AND object_id > 5911
于 2013-06-15T05:40:21.457 回答
0

您的解决方案在这里...

 import java.sql.*;
   public class sample 
  {
public static void main(String[] args) {

 try{

    Class.forName("com.mysql.jdbc.Driver");
    Connection  c=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
    Statement s=c.createStatement();
    Statement dbInsert = c.createStatement();
    ResultSet r=s.executeQuery("SELECT object_id FROM wp_term_relationships WHERE term_taxonomy_id = (SELECT term_id FROM wp_terms WHERE name = 'Chicago') AND object_id > 5911");
   while(r.next())
   {

      dbInsert.executeUpdate("INSERT INTO wp_postmeta (meta_key, meta_value,post_id) VALUES ('sidebar','chicago','"+r.getInt("object_id")+"'");

   }


  }
  catch(Exception e)
  {
      System.out.println(e.getMessage());
  }
 }
 }
于 2013-06-15T05:39:27.733 回答