2012-06-19 94 views
2

我想知道是否有人能够帮助我。分组依据和MySQL查询位置

我正在使用以下查询从MySQL数据库正确检索Google标记数据。

SELECT l.locationid, f.locationid, l.locationname, l.osgb36lat, l.osgb36lon, count(f.locationid) as totalfinds 
    FROM detectinglocations as l 
    LEFT JOIN finds AS f ON l.locationid=f.locationid 
    GROUP BY l.locationid 

我现在试图通过增加where条款加入到这一点,更具体where userid='$userid'

我试过在查询中的各个位置添加这个额外的子句,我确信这可能只是一个初学者的错误,但我不能让查询工作。

我只是想知道是否有人可以看看这个,让我知道我要去哪里错了。

感谢和亲切的问候

回答

2

尝试在GROUP BY之前添加where子句。

+2

嗨@dcp,感谢您花时间回复我的帖子并同意提供帮助。我已经尝试了您的建议,而且它的效果非常出色。当延迟的时间已经过去时,我会将此作为我接受的答案。再次非常感谢和问候 – IRHM

0
$query = "SELECT l.locationid, f.locationid, l.locationname, l.osgb36lat, l.osgb36lon, count(f.locationid) as totalfinds 
      FROM detectinglocations as l 
      LEFT JOIN finds as f on l.locationid=f.locationid 
      WHERE userid=".(int)$userid." 
      GROUP BY l.locationid"; 

我也开始加入(INT)到您的变量 “的sanitize” 的不明(对我来说)VAR

+0

您好@Luis Siqot,感谢您花时间回复我的文章并提供帮助。我试过你建议的代码,不幸的是我无法使查询工作。然而,我接受了以前的建议,因为这个工作绝对没问题。亲切的问候 – IRHM

+0

我们所有人都在这里帮助,请注意,我的anwser是一样的接受anwser,加上建议要小心sql注入。 –

+0

谢谢你的建议。 – IRHM

1

连接中,WHERE条件来后ON声明

如果userid是那里表detectinglocations然后用

SELECT l.locationid, f.locationid, l.locationname, l.osgb36lat, l.osgb36lon, count(f.locationid) as totalfinds 
FROM detectinglocations as l 
LEFT JOIN finds AS f ON l.locationid=f.locationid 
WHERE l.userid='$userid' 
GROUP BY l.locationid 

如果userid有表finds则u se

SELECT l.locationid, f.locationid, l.locationname, l.osgb36lat, l.osgb36lon, count(f.locationid) as totalfinds 
FROM detectinglocations as l 
LEFT JOIN finds AS f ON l.locationid=f.locationid 
WHERE f.userid='$userid' 
GROUP BY l.locationid 
+0

Hi @Fahim Parkar,感谢您花时间帮助我解决这个问题。你的建议几乎反映了我收到的另一个答案,它的效果很好。亲切的问候 – IRHM

+1

不是问题..你可以接受dcp答案.. –

+0

非常感谢。问候 – IRHM