子查询返回超过1行?

时间:2016-07-13 03:59:33

标签: mysql sql

子查询返回超过1行?闷闷不乐?



SELECT `t_files`.*, `t_users`.`username`, 
(SELECT CONCAT(b.first_name, " ", b.last_name)
	FROM t_files AS a
	JOIN t_users as b 
          ON b.id = a.user_id 
) as upload_by 
FROM `t_files` 
LEFT JOIN `t_files_permission` 
   ON `t_files_permission`.`id_files` = `t_files`.`file_id` 
LEFT JOIN `t_users` 
   ON `t_users`.`id` = `t_files_permission`.`id_users`
WHERE `t_files`.`company_id` = '1' 
  AND `t_files_permission`.`id_users` = '59' 
  AND `is_deleted` =0




2 个答案:

答案 0 :(得分:0)

在子查询中添加条件,如下所示

SELECT t_files。*,t_usersusername, (SELECT CONCAT(b.first_name,"",b.last_name)     FROM t_files AS a      JOIN t_users AS b ON b.id = a.user_id

  

在哪里a.id_users` =' 59'

)作为upload_by 来自t_files LEFT JOIN t_files_permission点击t_files_permissionid_files = t_filesfile_id LEFT JOIN t_users点击t_usersid = t_files_permissionid_userst_filescompany_id =' 1'和t_files_permissionid_users =' 59' AND is_deleted = 0

答案 1 :(得分:0)

试试这种方式

SELECT `t_files`.*, `t_users`.`username`, 
(select CONCAT(b.first_name, " ", b.last_name)
    from t_files as a
     join t_users as b on b.id = a.user_id 
     where t.files.id=a.id) as upload_by 
FROM `t_files` as 
LEFT JOIN `t_files_permission` ON `t_files_permission`.`id_files` = `t_files`.`file_id` 
LEFT JOIN `t_users` ON `t_users`.`id` = `t_files_permission`.`id_users`
WHERE `t_files`.`company_id` = '1' AND `t_files_permission`.`id_users` = '59' AND `is_deleted` =0

我认为你不需要子查询。您可以使用联接来解决问题。