select 1 random row with complex filtering

前端 未结 4 1919
死守一世寂寞
死守一世寂寞 2021-01-24 05:11

I\'ve 2 tables:

first table users:

+-------------------------+---------+------+-----+---------+-------+
| Field                   | Type             


        
4条回答
  •  生来不讨喜
    2021-01-24 05:57

    Have you tried switching not exists to left join?

    SELECT DISTINCT *
    FROM   profiles t1
    LEFT JOIN
           proposal t2
    ON     t1.id = t2.to_id
    WHERE  t1.first_name IS NOT NULL AND
           t2.to_id IS NULL
    ORDER BY RAND()
    LIMIT 0 , 1
    

    This will return you all rows of profiles, and to those that are not matched by a row in proposal it will assign NULL values, on which you can filter.

    The result should be the same, but the performance may be better.

提交回复
热议问题