2

I have a Wordpress site which has post ratings stored in the wp_ratings table, with each rating stored as an individual row. We will be changing the theme soon and need to convert the values stored in wp_ratings to meta data, which will look as follows:

a: 3: {
i: 0;
a: 2: {
    s: 7: "user_id";
    s: 1: "0";
    s: 2: "ip";
    s: 9: "127.0.0.1";
}
i: 1;
a: 2 {
    s: 7: "user_id";
    s: 1: "0";
    s: 2: "ip";
    s: 9: "127.0.0.1";
}
i: 2;
a: 2: {
    s: 7: "user_id";
    s: 1: "0";
    s: 2: "ip";
    s: 9: "127.0.0.1";
}
i: 3;
a: 2: {
    s: 7: "user_id";
    s: 1: "0";
    s: 2: "ip";
    s: 9: "127.0.0.1";
}

The total vote value is show by the a:3 at the beginning of the statement and then there is an individual entry for each rating stored.

I am not particularly familiar with stored procedures but I want to create a loop that will calculate the total value for each post in wp_posts sum(rating_rating) and then create the meta data into one long string.

I don't really know where to start with this, so if anyone can point me in the right direction, I'd be really grateful.

Thanks

5
  • There's a semi-colon missing on line 10. (I don't have enough privileges to correct it.) The first brace opened is never closed. This leaves solutions open to ambiguity!
    – jon
    Jan 22, 2012 at 16:36
  • The first brace encompasses all the individual entries within it. The closing brace should be at the every end of the statement. Jan 22, 2012 at 17:06
  • Are you saying the first brace doesn't need to be closed (because it's not)? Can you post the meta data specs?
    – jon
    Jan 22, 2012 at 17:15
  • Unfortunately I don't have a meta data spec per se, but this forum thread should explains how it works. cosmothemes.com/forums/topic/436 Jan 22, 2012 at 17:56
  • Also jonlester, yes, the first brace should be closed - I left it out in error. Jan 22, 2012 at 17:57

2 Answers 2

1

Setup test data

CREATE TABLE `wp_ratings` ( 
  `rating_id` INT(11) NOT NULL AUTO_INCREMENT, 
  `rating_postid` INT(11) NOT NULL, 
  `rating_posttitle` TEXT NOT NULL, 
  `rating_rating` INT(2) NOT NULL, 
  `rating_timestamp` VARCHAR(15) NOT NULL, 
  `rating_ip` VARCHAR(40) NOT NULL, 
  `rating_host` VARCHAR(200) NOT NULL, 
  `rating_username` VARCHAR(50) NOT NULL, 
  `rating_userid` INT(10) NOT NULL DEFAULT '0', 
  PRIMARY KEY  (`rating_id`), 
  KEY `rating_postid` (`rating_postid`) 
); 

INSERT INTO `test`.`wp_ratings` 
  (`rating_id`, `rating_postid`, `rating_posttitle`, 
  `rating_rating`, `rating_timestamp`, `rating_ip`, 
  `rating_host`, `rating_username`, `rating_userid`)
  VALUES
  (1,1,'title',1,'abc','127.0.0.1','a.a.a','user_id',1),
  (2,2,'title',1,'abc','127.0.0.1','a.a.a','user_id',1),
  (3,2,'title',1,'abc','127.0.0.1','a.a.a','user_id',1),
  (4,3,'title',1,'abc','127.0.0.1','a.a.a','user_id',1),
  (5,3,'title',1,'abc','127.0.0.1','a.a.a','user_id',1),
  (6,3,'title',1,'abc','127.0.0.1','a.a.a','user_id',1);

Query

SET @post_id = 3;

SELECT CONCAT
(
'a:', COUNT(rating_id), ':{',
    (
        SELECT CONCAT( GROUP_CONCAT(meta_data_vote SEPARATOR ''), '}') FROM
        (
            SELECT CONCAT
            ( 
                'i:',
                @curRow := @curRow + 1,
                ';a:2:{s:7:"', 
                rating_username, 
                '";s:1:"0";s:2:"ip";s:9:"', 
                rating_ip,
                '";}'
            )AS meta_data_vote
            FROM
                wp_ratings
            JOIN (SELECT @curRow := -1 AS j) r
            WHERE rating_postid = @post_id
        )AS meta_data_votes
    )
) AS new_ratings_meta_data
FROM wp_ratings l
WHERE rating_postid = @post_id

Result (where @post_id = 3)

a:3:{i:0;a:2:{s:7:"user_id";s:1:"0";s:2:"ip";s:9:"127.0.0.1";}i:1;a:2:{s:7:"user_id";s:1:"0";s:2:"ip";s:9:"127.0.0.1";}i:2;a:2:{s:7:"user_id";s:1:"0";s:2:"ip";s:9:"127.0.0.1";}}

Conclusion

I haven't been able to write a query that returns a resultset with post_ids and their new meta_data.

The above code will only convert on a post-by-post basis.

If you want to batch update, it'll need more thought, but i think this will get you off to a good start.

7
  • Hi, Thanks, this is brilliant. One problem though - there are some posts that have been rated more than 1500 times, so the approximate length of the meta data for those posts may be around 90,000 characters. This solution works where there are very few votes, so how do I get the query to the strings in their entirety? Jan 23, 2012 at 11:10
  • When I run the query in MySQL Workbench it just returns the string as 'BLOB' and in Sequel Pro it gets as far as the fourth vote and then goes no further. Don't know if this is just a display limit or not. Might try inserting the value into another table to see if it returns the complete string. Jan 23, 2012 at 19:40
  • @Andy, try: SET SESSION group_concat_max_len = 1000000;
    – jon
    Jan 23, 2012 at 20:07
  • perfect! one final problem - I have tweaked it to show the results grouped by post_id and want to import this into the 'wp_postmeta' table, but when I use an insert select statement, I get an error. Will post the query below: Jan 24, 2012 at 22:53
  • SET SESSION group_concat_max_len = 1000000; INSERT INTO wp_postmeta ('post_id','meta_key','meta_value') SELECT rating_postid ,'like' ,CONCAT ( 'a:', sum(rating_rating), ':{', ( SELECT CONCAT( GROUP_CONCAT(meta_data_vote SEPARATOR ''), '}') FROM ( SELECT CONCAT ( 'i:', @curRow := @curRow + 1, ';a:2:{s:7:"', 'user_id', '";s:1:"0";s:2:"ip";s:16:"', '-127-000-000-001', '";}' )AS meta_data_vote Jan 24, 2012 at 22:57
0

I don't know much about stored procedures, but I think you can do this with just a single query instead, using group_concat() and group by post id.

3
  • Would that work? the 'i' value at the beginning of each individual rating doesn't exist within the database at the moment, so I think would require a variable used as a counter...? Jan 22, 2012 at 17:07
  • No, use the COUNT(*) function an it will return the number if rows grouped together. Jan 22, 2012 at 17:11
  • You can literally write COUNT(*) in your query. Jan 22, 2012 at 17:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.