PHP MySQL - IN / ALL / ANY子查询中的列'id'是不明确的

时间:2013-02-23 15:43:26

标签: php mysql

我目前有以下查询工作:

$sql = $db->Query("SELECT a.id FROM surf a LEFT JOIN users b ON b.id = a.user LEFT JOIN surfed c ON c.user = '".$data['id']."' AND c.site = a.id WHERE a.active = '0' AND (b.coins >= a.cpc AND a.cpc >= '2') AND (c.site IS NULL AND a.user !='".$data['id']."')".$dbt_value." ORDER BY a.cpc DESC LIMIT 1");

但是我想在其中添加以下AND语句:

 AND `cpc`<='10' AND (`id` NOT IN (SELECT `site` FROM `surfed_site`)) 

所以我看起来像:

$sql = $db->Query("SELECT a.id FROM surf a LEFT JOIN users b ON b.id = a.user LEFT JOIN surfed c ON c.user = '".$data['id']."' AND c.site = a.id WHERE a.active = '0' AND (b.coins >= a.cpc AND a.cpc >= '2') AND `cpc`<='10' AND (`id` NOT IN (SELECT `site` FROM `surfed_site`)) AND (c.site IS NULL AND a.user !='".$data['id']."')".$dbt_value." ORDER BY a.cpc DESC LIMIT 1");

但接着我收到了这条消息:

IN / ALL / ANY子查询中的列'id'是不明确的

我的查询有什么问题?

4 个答案:

答案 0 :(得分:6)

尝试更改

AND `cpc`<='10' AND (`id` NOT IN (SELECT `site` FROM `surfed_site`)) 

AND `cpc`<='10' AND (`a.id` NOT IN (SELECT `site` FROM `surfed_site`)) 

您需要a.id,因为它不确定要在哪个表中引用id列。

答案 1 :(得分:1)

出现该错误的原因是因为有多个表包含列ID。为了执行查询,您必须提供列ID所在的表,以便服务器识别哪个表与之相关,例如

AND `cpc`<='10' AND (a.`id` NOT IN (SELECT `site` FROM `surfed_site`)) 

如果您的意思是您要比较的ID来自表surf

答案 2 :(得分:1)

您必须指定所需的ida.idb.id等。

答案 3 :(得分:0)

另外我假设$ data包含转义数据(单引号)?如果不是这是第二个问题。