×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: SQL
Posted by: Fabian Constien
Added: Nov 27, 2017 4:02 PM
Views: 2
Tags: no tags
  1. SELECT
  2.     `emails`.`email` AS `email`, COUNT(*) AS `email_num`
  3. FROM
  4.     `users`
  5.         LEFT JOIN
  6.     `emails` ON (`emails`.`users_id` = `users`.`id`
  7.         AND `emails`.`id` = (SELECT
  8.             MAX(`id`)
  9.         FROM
  10.             `emails`
  11.         WHERE
  12.             `emails_types_id` = 2
  13.                 AND `users_id` = `users`.`id`))
  14. GROUP BY `email`
  15. HAVING `email_num` > 1
  16. ORDER BY `email`;