如何从子查询中选择更多行

时间:2012-10-31 22:40:52

标签: mysql

当我尝试运行此查询时:

 select * FROM `activity` 
 WHERE user_id = 1
 AND activity_id NOT LIKE (select activity_id from activity where user_id = 1 ORDER BY activity_id DESC LIMIT 8)

我收到以下错误:

 Subquery returns more than 1 row

我该如何解决这个问题?我想从表中选择activity_id,不包括特定用户的最新8个activity_id。

2 个答案:

答案 0 :(得分:3)

NOT LIKE期望比较表达式或值而不是结果集。 改变NOT LIKE for NOT IN

试试这个:

    SELECT * FROM `activity` 
             WHERE user_id = 1 AND activity_id NOT IN (
                     SELECT activity_id FROM activity WHERE user_id = 1 
                                        ORDER BY activity_id DESC LIMIT 8)

答案 1 :(得分:0)

通过这样做解决了这个问题:

                $sql2 = "DELETE t1.*
                        FROM activity t1
                        left join (select activity_id from activity where user_id = '".$row['user_id']."' ORDER BY activity_id DESC LIMIT 8) t2
                        on (t1.activity_id = t2.activity_id)
                        where t2.activity_id is null
                        and t1.user_id = '".$row['user_id']."'";