Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 5th, 2012  |  syntax: None  |  size: 2.28 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Looping in MySQL to create a concatenated variable
  2. a: 3: {
  3. i: 0;
  4. a: 2: {
  5.     s: 7: "user_id";
  6.     s: 1: "0";
  7.     s: 2: "ip";
  8.     s: 9: "127.0.0.1";
  9. }
  10. i: 1;
  11. a: 2 {
  12.     s: 7: "user_id";
  13.     s: 1: "0";
  14.     s: 2: "ip";
  15.     s: 9: "127.0.0.1";
  16. }
  17. i: 2;
  18. a: 2: {
  19.     s: 7: "user_id";
  20.     s: 1: "0";
  21.     s: 2: "ip";
  22.     s: 9: "127.0.0.1";
  23. }
  24. i: 3;
  25. a: 2: {
  26.     s: 7: "user_id";
  27.     s: 1: "0";
  28.     s: 2: "ip";
  29.     s: 9: "127.0.0.1";
  30. }
  31.        
  32. CREATE TABLE `wp_ratings` (
  33.   `rating_id` INT(11) NOT NULL AUTO_INCREMENT,
  34.   `rating_postid` INT(11) NOT NULL,
  35.   `rating_posttitle` TEXT NOT NULL,
  36.   `rating_rating` INT(2) NOT NULL,
  37.   `rating_timestamp` VARCHAR(15) NOT NULL,
  38.   `rating_ip` VARCHAR(40) NOT NULL,
  39.   `rating_host` VARCHAR(200) NOT NULL,
  40.   `rating_username` VARCHAR(50) NOT NULL,
  41.   `rating_userid` INT(10) NOT NULL DEFAULT '0',
  42.   PRIMARY KEY  (`rating_id`),
  43.   KEY `rating_postid` (`rating_postid`)
  44. );
  45.  
  46. INSERT INTO `test`.`wp_ratings`
  47.   (`rating_id`, `rating_postid`, `rating_posttitle`,
  48.   `rating_rating`, `rating_timestamp`, `rating_ip`,
  49.   `rating_host`, `rating_username`, `rating_userid`)
  50.   VALUES
  51.   (1,1,'title',1,'abc','127.0.0.1','a.a.a','user_id',1),
  52.   (2,2,'title',1,'abc','127.0.0.1','a.a.a','user_id',1),
  53.   (3,2,'title',1,'abc','127.0.0.1','a.a.a','user_id',1),
  54.   (4,3,'title',1,'abc','127.0.0.1','a.a.a','user_id',1),
  55.   (5,3,'title',1,'abc','127.0.0.1','a.a.a','user_id',1),
  56.   (6,3,'title',1,'abc','127.0.0.1','a.a.a','user_id',1);
  57.        
  58. SET @post_id = 3;
  59.  
  60. SELECT CONCAT
  61. (
  62. 'a:', COUNT(rating_id), ':{',
  63.     (
  64.         SELECT CONCAT( GROUP_CONCAT(meta_data_vote SEPARATOR ''), '}') FROM
  65.         (
  66.             SELECT CONCAT
  67.             (
  68.                 'i:',
  69.                 @curRow := @curRow + 1,
  70.                 ';a:2:{s:7:"',
  71.                 rating_username,
  72.                 '";s:1:"0";s:2:"ip";s:9:"',
  73.                 rating_ip,
  74.                 '";}'
  75.             )AS meta_data_vote
  76.             FROM
  77.                 wp_ratings
  78.             JOIN (SELECT @curRow := -1 AS j) r
  79.             WHERE rating_postid = @post_id
  80.         )AS meta_data_votes
  81.     )
  82. ) AS new_ratings_meta_data
  83. FROM wp_ratings l
  84. WHERE rating_postid = @post_id
  85.        
  86. 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";}}