flappyfish
2017-11-14 02:45
采纳率: 100%
浏览 3.2k

mybatis执行select子查询分页报错

select
    adr.modifiedname,
    adr.modifiedon,
    adr.comment_tag,
    adr.post_desc,
    adr.qualifications,
    (
    SELECT
                GROUP_CONCAT(r.user_name)
                FROM
                post_recruiter_related r
                WHERE
                r.post_id =  ah.post_id
                AND r.isdeleted = 0
    ) AS recruitPersons
    from ad_headhunter_related ah LEFT JOIN ad_release_record adr ON ah.post_id = adr.post_id
    where adr.isdeleted=0
   HAVING recruitPersons like '%杰%'
    ORDER by ah.create_time DESC

我有一条sql语句,其中select里面有一个子句关联另一张表,这条sql单独执行没问题,但是配合PageHelper分页就报错,提示“ SELECT count(0) FROM ad_headhunter_related ah LEFT JOIN ad_release_record adr ON ah.post_id = adr.post_id WHERE adr.isdeleted = 0 AND ah.is_deleted = 0 AND ah.hunter_id = 83 AND adr.post_nameE '%杰%' HAVING recruitPerons LIKE CONCAT('
%', ?, '%')
提示找不到recruitPersons这个列,这是什么原因呢?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

6条回答 默认 最新

  • sheng94458 2017-11-14 03:41
    已采纳

    SELECT count(0) FROM ad_headhunter_related ah LEFT JOIN ad_release_record adr ON ah.post_id = adr.post_id WHERE adr.isdeleted = 0 AND ah.is_deleted = 0 AND ah.hunter_id = 83 AND adr.post_nameE '%杰%' HAVING recruitPerons LIKE CONCAT('
    %', ?, '%')

    这个sql肯定是会提示找不到recruitPersons这个列,这句sql应该是分页时查询数据总数的吧?你可以试试这样改改,因为你查询数据总数时,并没有把recruitPersons这列查询出来,这个是子查询出来的临时字段并不是数据库字段

         select count(*) from (SELECT  (SELECT GROUP_CONCAT(r.user_name) FROM post_recruiter_related r WHERE r.post_id =  ah.post_id AND r.isdeleted = 0) AS recruitPersons FROM ad_headhunter_related ah LEFT JOIN ad_release_record adr ON ah.post_id = adr.post_id WHERE adr.isdeleted = 0 
    

    AND ah.is_deleted = 0 AND ah.hunter_id = 83 AND adr.post_nameE '%杰%' HAVING recruitPerons LIKE CONCAT('%', ?, '%')) as tab1

    已采纳该答案
    打赏 评论
  • flappyfish 2017-11-14 02:51

    就是他在执行count时没有查询我这个子句,但是我这个子句又是后面的过滤条件

    打赏 评论
  • 砸死接触 2017-11-14 03:16

    sql语句执行顺序是先having 再select 当然没有这个列啊!

    打赏 评论
  • 砸死接触 2017-11-14 03:20

    一个语句的执行顺序 from-- on---join--where--group by-- with --having-- select --distinnct --order by --limit 楼主看看 楼主的语句适合这个顺序吗? 我看不适合

    打赏 评论
  • 狗子猴子驴子 2017-11-14 03:47

    where 条件 放在最后边

    打赏 评论
  • qq_25314915 2017-11-14 08:33

    select
    ah.post_id,
    adr.modifiedname,
    adr.modifiedon,
    adr.comment_tag,
    adr.post_desc,
    adr.qualifications

    (
    SELECT
                GROUP_CONCAT(r.user_name)
                FROM
                post_recruiter_related r
                WHERE
                r.post_id =  ah.post_id
                AND r.isdeleted = 0
    ) AS recruitPersons
    from ad_headhunter_related ah LEFT JOIN ad_release_record adr ON ah.post_id = adr.post_id
    where adr.isdeleted=0
    

    and recruitPersons like '%杰%'
    ORDER by ah.create_time DESC

        感觉像子查询的条件没满足  你试试
    
    打赏 评论

相关推荐 更多相似问题