mysql group by uid哪个uid与另一个查询匹配

mysql group by uid哪个uid与另一个查询匹配,mysql,group-by,Mysql,Group By,我想得到10兰特的结果,这image!='',按uid分组,这些uid与select uid from user\u table,type='1'中的相同。但是我的查询只返回2个结果。问题在哪里 select * from article_table where image!='' order by rand() group by uid in (select uid from user_table where type='1') limit 10 我会用一个join来代替 selec

我想得到10兰特的结果,这
image!='',按
uid
分组,这些
uid
select uid from user\u table,type='1'
中的相同。但是我的查询只返回2个结果。问题在哪里

select * from article_table where image!='' 
order by rand() 
group by uid 
in (select uid from user_table where type='1') 
limit 10

我会用一个
join
来代替

select *
  from article_table at
  join ( select uid
           from user_table
          where type = '1' ) ut
    on at.uid = ut.uid
 where image != ''
 group by at.uid
 order by rand()
 limit 10
或者,您可能希望限制
user\u表
中的
uid
s的数量,以便更快地开始:

select at.*
  from article_table at
  join ( select uid
           from user_table
          where type = '1'
          order by rand()
          limit 10 ) ut
    on at.uid = ut.uid
 where image != ''
 group by at.uid
 order by rand()
 limit 10
我在这里假设每个用户都有很多文章。尽管看起来更可怕,但内部选择中的
order by rand()
是在一个较小的数据集之上的,这将加快速度,而外部选择中的
order by
只需处理较少的行数


请小心,按随机值排序可能会导致严重的性能损失,因为您必须遍历与where子句匹配的整个表。有。

我会用一个
连接来代替

select *
  from article_table at
  join ( select uid
           from user_table
          where type = '1' ) ut
    on at.uid = ut.uid
 where image != ''
 group by at.uid
 order by rand()
 limit 10
或者,您可能希望限制
user\u表
中的
uid
s的数量,以便更快地开始:

select at.*
  from article_table at
  join ( select uid
           from user_table
          where type = '1'
          order by rand()
          limit 10 ) ut
    on at.uid = ut.uid
 where image != ''
 group by at.uid
 order by rand()
 limit 10
我在这里假设每个用户都有很多文章。尽管看起来更可怕,但内部选择中的
order by rand()
是在一个较小的数据集之上的,这将加快速度,而外部选择中的
order by
只需处理较少的行数


请小心,按随机值排序可能会导致严重的性能损失,因为您必须遍历与where子句匹配的整个表。有。

以下查询将完成此操作

SELECT * 
FROM   article_table 
WHERE  image!='' 
       AND uid IN (SELECT DISTINCT uid 
                   FROM   user_table 
                   WHERE  TYPE = '1' 
                   LIMIT  10) 
GROUP  BY uid 
ORDER  BY Rand() 
LIMIT  10 

下面的查询将执行此操作

SELECT * 
FROM   article_table 
WHERE  image!='' 
       AND uid IN (SELECT DISTINCT uid 
                   FROM   user_table 
                   WHERE  TYPE = '1' 
                   LIMIT  10) 
GROUP  BY uid 
ORDER  BY Rand() 
LIMIT  10