sql 查询一个表信息 条件为该表一个字段大于对应(必须该表相对应的Id)另外一个表的总数

时间:2021-10-25 15:10:10

解决方案一:

 把查询的总数当一个子表 然后再条件链接查询     (10万以上的记录)查询时间4.796s   效率差

select  * from tech_sys_user t2,
( SELECT
t.id as id2,(
SELECT
count(o.id) AS dd
FROM
`tech_user_attention` o
WHERE
o.uid = t.id
) AS xx
FROM
tech_sys_user t ) as temp2 where t2.Id=temp2.id2 and t2.attentions <> temp2.xx

解决方案二:

直接在条件里面查询 注意 必须tech_sys_user As t 不然条件 t.id 访问无效  (10万以上的记录)查询时间0.063s   效率高

select  * from tech_sys_user As t
where   t.attentions <> (
SELECT
count(o.id) AS dd
FROM
`tech_user_attention` o
WHERE
o.uid = t.id
)

错误方案:  查询出错

select  * from tech_sys_user t
where   t.attentions <> (
SELECT
count(o.id) AS dd
FROM
`tech_user_attention` o
WHERE
o.uid = t.id
)