0

我目前有以下查询工作:

$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

4 回答 4

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要引用哪个表中的哪一列。

于 2013-02-23T15:45:54.863 回答
1

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

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

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

于 2013-02-23T15:46:06.977 回答
1

您必须指定id您想要的。a.idb.id

于 2013-02-23T15:47:29.700 回答
0

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

于 2013-02-23T15:50:15.740 回答