Advertisement
Guest User

Untitled

a guest
Aug 4th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 0.86 KB | None | 0 0
  1. $sql = "SELECT
  2.     s.*,
  3.     co.name AS county,
  4.    
  5.     ROUND ( AVG(r.rating ), 1 ) as rating_avg,
  6.  
  7.     CONCAT('[',
  8.         COALESCE(
  9.             GROUP_CONCAT(
  10.                 DISTINCT
  11.                 CONCAT('\"',c.name,'\"')
  12.                 ORDER BY c.name ASC
  13.                 SEPARATOR ','
  14.             ),
  15.         ''),
  16.     ']') AS categories,
  17.    
  18.     CONCAT('[',
  19.         COALESCE(
  20.             GROUP_CONCAT(
  21.                 DISTINCT
  22.                 CONCAT('{
  23.                     \"user\":\"',u.nickname,'\",
  24.                     \"rating\":\"',r.rating,'\",
  25.                     \"date\":\"',r.date,'\",
  26.                     \"comment\":\"',
  27.                         REPLACE(
  28.                             REPLACE(r.comment, '\n', '\\\\n'),
  29.                             '\"', '\\\\\"' ),'\"
  30.                 }')
  31.                 SEPARATOR ','
  32.             ),
  33.         ''),
  34.     ']') AS ratings
  35.    
  36.     FROM services s
  37.    
  38.     JOIN ratings r ON r.service_id = s.id
  39.     JOIN users u ON u.user_id = r.user_id
  40.     JOIN category_relationships cr ON cr.service_id = s.id
  41.     JOIN categories c ON c.id = cr.category_id
  42.     JOIN counties co ON co.id = s.county
  43.    
  44.     WHERE s.id = '$sid' ";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement