MYSQL delete all results having count(*)=1

后端 未结 3 552
天命终不由人
天命终不由人 2020-12-15 01:19

I have a table taged with two fields sesskey (varchar32 , index) and products (int11), now I have to delete all rows that having group by sesskey count(*) = 1. I\'m trying a

3条回答
  •  悲哀的现实
    2020-12-15 02:21

    DELETE  si
    FROM    t_session si
    JOIN    (
            SELECT  sesskey
            FROM    t_session so
            GROUP BY
                    sesskey
            HAVING  COUNT(*) = 1
            ) q
    ON      q.sesskey = si.sesskey
    

    You need to have a join here. Using a correlated subquery won't work.

    See this article in my blog for more detail:

    • Keeping rows

提交回复
热议问题