SELECT *,
(SELECT name FROM storage_bin WHERE id=storage_files.bin_id) AS bin_name,
CONCAT('foMOV',id) as foMOV
FROM
storage_files
WHERE
(user_id = 0 ||
(
(SELECT COUNT(id) FROM storage_files_rights_user WHERE file_id=storage_files.id) <= 0
AND
(SELECT COUNT(id) FROM storage_files_rights_group WHERE file_id=storage_files.id) <= 0
AND
(SELECT COUNT(id) FROM storage_bin_rights_group WHERE bin_id=storage_files.bin_id) <= 0
)
||
(
SELECT count(storage_files.id)
FROM storage_files, storage_bin_rights_group, usergroups_user
WHERE storage_bin_rights_group.bin_id = storage_files.bin_id
AND storage_bin_rights_group.usergroup_id = usergroups_user.usergroup_id
AND usergroups_user.user_id = 0
)
)
ORDER BY
realfilename ASC