2

我想知道是否有人可以帮助我。

我正在使用下面的查询从 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'

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

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

谢谢和亲切的问候

4

3 回答 3

2

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

于 2012-06-19T12:50:51.273 回答
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然后使用

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
于 2012-06-19T12:58:10.613 回答
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) 以“清理”一个未知的(对我而言)变量

于 2012-06-19T12:52:25.423 回答