有一张表t_card_question
字段 card_id (答题卡UID)
question_id(题目UID)
card_question_index(题目在一张答题卡上对应的编号)(最大值不固定)描述:表中有多张答题卡(多个card_id)要求查询出:表中每个card_id对应的最大card_question_index的question_id

解决方案 »

  1.   

    select card_id,question_id
    from t_card_question a
    where not exists (select from t_card_question where card_id=a.card_id and card_question_index>a.card_question_index)
      

  2.   

    另外还有几种写法,功能一样,效率上略有差别。select card_id,question_id
    from t_card_question a
    where card_question_index=(select max(card_question_index) from t_card_question where card_id=a.card_id)select a.card_id,a.question_id
    from t_card_question a inner join (select card_id,max(card_question_index) as card_question_index from t_card_question group by card_id) b
    on a.card_id=b.card_id and a.card_question_index=b.card_question_index