Skip to content

Instantly share code, notes, and snippets.

@etianen
Created November 16, 2010 18:31
Show Gist options
  • Save etianen/702232 to your computer and use it in GitHub Desktop.
Save etianen/702232 to your computer and use it in GitHub Desktop.
SELECT
(
IF (
(
SELECT COUNT(*)
FROM `people_person_contacts`
WHERE `from_person_id` = 12895 AND `to_person_id` = `people_person`.`id`
),
1,
IF (
(
(
SELECT COUNT(*)
FROM `organizations_personorganizationrole`
WHERE `person_id` = `people_person`.`id` AND `organization_id` IN (
SELECT `id`
FROM `organizations_organization`
WHERE `id` IN (
SELECT `organization_id`
FROM `organizations_personorganizationrole`
WHERE `person_id` = 12895
)
)
) OR (
SELECT COUNT(*)
FROM `projects_personprojectrole`
WHERE `person_id` = `people_person`.`id` AND `project_id` IN (
SELECT `id`
FROM `projects_project`
WHERE `id` IN (
SELECT `project_id`
FROM `projects_personprojectrole`
WHERE `person_id` = 12895
)
)
) OR (
SELECT COUNT(*)
FROM `events_personeventrole`
WHERE `person_id` = `people_person`.`id` AND `status` = 2 AND `event_id` IN (
SELECT `id`
FROM `events_event`
WHERE `id` IN (
SELECT `event_id`
FROM `events_personeventrole`
WHERE `person_id` = 12895 and `status` = 2
) AND `date` < 2010-11-16 18:27:47.380759
)
)
),
2,
3
)
)
) AS `user_access`,
`people_person`.`id`,
`people_person`.`date_created`,
`people_person`.`date_modified`,
`people_person`.`access`,
`people_person`.`thumbnail`,
`people_person`.`description`,
`people_person`.`short_description`,
`people_person`.`title`,
`people_person`.`first_name`,
`people_person`.`initials`,
`people_person`.`last_name`,
`people_person`.`password_hash`,
`people_person`.`account_status`
FROM `people_person`
WHERE
`access` >= (
IF (
(
SELECT COUNT(*)
FROM `people_person_contacts`
WHERE `from_person_id` = 12895 AND `to_person_id` = `people_person`.`id`
),
1,
IF (
(
(
SELECT COUNT(*)
FROM `organizations_personorganizationrole`
WHERE `person_id` = `people_person`.`id` AND `organization_id` IN (
SELECT `id`
FROM `organizations_organization`
WHERE `id` IN (
SELECT `organization_id`
FROM `organizations_personorganizationrole`
WHERE `person_id` = 12895
)
)
) OR (
SELECT COUNT(*)
FROM `projects_personprojectrole`
WHERE `person_id` = `people_person`.`id` AND `project_id` IN (
SELECT `id`
FROM `projects_project`
WHERE `id` IN (
SELECT `project_id`
FROM `projects_personprojectrole`
WHERE `person_id` = 12895
)
)
) OR (
SELECT COUNT(*)
FROM `events_personeventrole`
WHERE `person_id` = `people_person`.`id` AND `status` = 2 AND `event_id` IN (
SELECT `id`
FROM `events_event`
WHERE `id` IN (
SELECT `event_id`
FROM `events_personeventrole`
WHERE `person_id` = 12895 and `status` = 2
) AND `date` < 2010-11-16 18:27:47.380759
)
)
),
2,
3
)
)
)
ORDER BY
`people_person`.`last_name` ASC,
`people_person`.`first_name` ASC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment