itruf

Untitled

Sep 5th, 2017
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 1.44 KB | None | 0 0
  1. SELECT
  2.    region.`name`,
  3.    u2.username AS chief,
  4.    user.username AS supervisor,
  5.    user.id AS supervisor_id,
  6.    Sum(s1.count_photo) AS sum,
  7.    (
  8.       SELECT
  9.          COUNT(*)
  10.       FROM
  11.          (
  12.             SELECT
  13.                Count(photo.id) as c
  14.             FROM
  15.                photo
  16.                INNER JOIN
  17.                   report
  18.                   ON photo.report_id = report.id
  19.                INNER JOIN
  20.                   availability
  21.                   ON report.availability_id = availability.id
  22.                INNER JOIN
  23.                   place as place2
  24.                   ON availability.place_id = place2.id
  25.             WHERE
  26.                place2.supervisor = place.supervisor AND
  27.                availability.removed IS NULL
  28.             GROUP BY
  29.                report.availability_id
  30.             HAVING
  31.                c = 1
  32.          )
  33.          x
  34.    ) AS Target
  35. FROM
  36.    furniture s1
  37.    INNER JOIN
  38.       availability
  39.       ON availability.furniture_id = s1.id
  40.    INNER JOIN
  41.       place
  42.       ON availability.place_id = place.id
  43.    INNER JOIN
  44.       user
  45.       ON place.supervisor = user.id
  46.    INNER JOIN
  47.       region
  48.       ON place.region_id = region.id
  49.    INNER JOIN
  50.       sector
  51.       ON place.sector_id = sector.id
  52.    INNER JOIN
  53.       user AS u2
  54.       ON sector.chief = u2.id
  55. WHERE
  56.    availability.removed IS NULL
  57.    AND s1.deleted_at IS NULL
  58. GROUP BY
  59.    place.supervisor
Advertisement
Add Comment
Please, Sign In to add comment