K'Tyn Fowler

Use ORDER BY to sort data

see comments

  • November 5, 2016 at 10:18 PM
  • Visible to public
/* query 1 */

SELECT
    articles.title,
    upper(articles.body) as content,
    articles.article_id,    
    COUNT(ReplyJoin.comment_id) as replies_count
FROM 
    articles
LEFT OUTER JOIN 
    comments AS ReplyJoin
ON 
    ReplyJoin.article_id = articles.article_id
WHERE 
    (articles.body like '%SWEET%' and articles.user_id < 4) or articles.title like '%blandit%'
GROUP BY 
    articles.article_id
HAVING 
    COUNT(ReplyJoin.comment_id) > 1 
ORDER BY 
    articles.title /*EVIDENCE HERE*/
;

/* query 2 */

SELECT
  users.name,
  comments.body as 'content',
  HEX(SUBSTRING_INDEX(comments.body, ' ', -1)) as 'secret',
  COUNT(users.name) as 'stack'
FROM
  comments
LEFT OUTER JOIN 
  users
ON 
  users.user_id = comments.user_id
LEFT OUTER JOIN 
  comments as parent_comment
ON 
  comments.parent_id = parent_comment.comment_id
WHERE
  ((comments.parent_id % 2) = 0 and users.name != 'jables') or comments.parent_id = 5
GROUP BY
  secret
HAVING
  secret != HEX('billy') and stack = 2
ORDER BY
  secret DESC, users.name ASC /*EVIDENCE HERE*/
;

/* bonus query */
SELECT
    lower(articles.title) as lower_title,   
    GROUP_CONCAT(comments.body SEPARATOR ' + ') AS comment_summary,
    articles.article_id
FROM 
    articles
LEFT OUTER JOIN 
    comments
ON 
    comments.article_id = articles.article_id
WHERE
    articles.article_id < 5 and LENGTH(articles.body) > 50 and comments.parent_id is not NULL
GROUP BY 
    articles.article_id
HAVING
    NOT comment_summary like '%bart%'  /*EVIDENCE HERE*/
ORDER BY
    LENGTH(articles.body) ASC /*EVIDENCE HERE*/
;