如何调整此查询?

时间:2011-06-16 09:35:48

标签: php mysql sql

这是我的查询

SELECT DISTINCT a.id,a.venue_id,a.user_id,mm.profilenam,mm.photo_thumb 
  FROM announce_arrival AS a,members as mm 
 WHERE a.user_id = mm.mem_id AND a.venue_id=mm.mem_id

我有venue_id和user_id,我想要将两个ID与成员表mem_id映射,这将返回我的profilename和photo_thumb。两个ID都是不同的,它们必须是不同的。


我喜欢这个答案:

SELECT DISTINCT a.id
               , a.venue_id
               , a.user_id
               , m1.profilenam
               , m2.photo_thumb
 FROM announce_arrival AS a
      , members as m1
      , members as m2  
 WHERE a.user_id = m1.mem_id  
 AND   a.venue_id=m2.mem_id

但是我不希望a.id在那里,我只想要venue_id和user_id以及我得到的结果我想要每一行。

 SELECT DISTINCT COUNT(*) 
 FROM announce_arrival AS a       
 INNER JOIN members as m1 ON (a.user_id = m1.mem_id)
 INNER JOIN members as m2 ON (a.venue_id= m2.mem_id)
 GROUP BY a.venue_id, a.user_id  ORDER BY date DESC,time DESC;

我如何在上述查询中使用count?

4 个答案:

答案 0 :(得分:0)

如果您只想使用profilename和photo thumb获取两个ID,首先应从查询中删除a.id并使用JOIN

SELECT DISTINCT a.venue_id,a.user_id,mm.profilenam,mm1.photo_thumb
FROM announce_arrival a
LEFT JOIN members mm ON a.user_id = mm.mem_id
LEFT JOIN members mm1 ON a.venue_id = mm1.mem_id

但对我来说,似乎比你有奇怪的数据库方案和奇怪的参考资料。

答案 1 :(得分:0)

也许

SELECT DISTINCT 
    announce_arrival.id,
    announce_arrival.venue_id,
    announce_arrival.user_id,
    members.profilenam,
    members.photo_thumb    
FROM announce_arrival 
INNER JOIN members ON members.mem_id = announce_arrival.user_id
INNER JOIN venue ON venue.id = announce_arrival.venue_id

但没有架构,我们只是猜测......

编辑:

SELECT DISTINCT Count(*)
FROM announce_arrival 
INNER JOIN members ON members.mem_id = announce_arrival.user_id
INNER JOIN venue ON venue.id = announce_arrival.venue_id

答案 2 :(得分:0)

我试图解释破坏的查询。

我对设计的解释是:

  1. for announce_arival
  2. 有一个成员记录user_id = mem_id
  3. 并且有一个成员记录了venue_id = mem_id
  4.  SELECT DISTINCT a.id
                   , a.venue_id
                   , a.user_id
                   , m1.profilenam
                   , m2.photo_thumb
     FROM announce_arrival AS a
          , members as m1
          , members as m2  
     WHERE a.user_id = m1.mem_id  
     AND   a.venue_id=m2.mem_id
    

    如果我误解了,请详细说明你的问题。

    我认为您的设计可能会将个人资料名称和photo_thumb放在不同的表格中。

答案 3 :(得分:0)

如果您想查看不同的场地和用户,请尝试此查询。

 SELECT 
   count(*) as rowcount
  , a.venue_id                
  , a.user_id                
  , m1.profilenam                
  , m2.photo_thumb  
 FROM announce_arrival AS a       
 INNER JOIN members as m1 ON (a.user_id = m1.mem_id)
 INNER JOIN members as m2 ON (a.venue_id= m2.mem_id)
 GROUP BY a.venue_id, a.user_id

如果 members.mem_id是主键(或唯一字段),则字段profilenamphoto_thumb依赖于这些字段,{{1}安全使用。

注意
如果您没有获得预期结果,则可能需要将group by中的一个或两个替换为inner join

关于不同
left joinDistinct非常相似,但工作方式不同。

有关详细信息,请参阅:http://dev.mysql.com/tech-resources/articles/debunking-group-by-myths.html

对数据库设计的批评
group byMembers.mem_id的{​​{1}}个链接。这看起来非常可疑,因为如果user_id恰好与某个venue_id相同,则无法告诉mem_id链接到user_id#10或venue_id#10或两者。
我建议您将announce_arrival.user_id拆分为两个字段:

announce_arrival.venue_id

如果您这样做,您的查询将变为:

mem_id

请注意,此处需要使用左连接,因为您希望显示与第一个连接或第二个连接或两者相匹配的行。
如果你使用members.venue_id -> links to announce_arrival.venue_id; is null if no link members.user_id -> links to announce_arrival.user_id; is null if no link. ,你将只获得两个连接产生匹配的行,这只会给出非常有限的行子集。

<强>计数
SELECT count(*) as rowcount , count(a.venue_id) as venue_count , count(a.user_id) as user_count , a.venue_id , a.user_id , m1.profilenam , m2.photo_thumb FROM announce_arrival AS a LEFT JOIN members as m1 ON (a.user_id = m1.user_id) LEFT JOIN members as m2 ON (a.venue_id = m2.venue_id) GROUP BY a.venue_id, a.user_id 计算推荐的所有行,因为它是可能的最快计数;它包括计数中的空行 inner join计算somefield不为null的所有行。