1

该错误仅显示部分查询,有人可以看到我犯的错误吗?

我想要的是基于标签获取条目,有 3 个表,nv_entries、nv_tags 和 nv_images。如果标签在 nv_tags 表中,那么它应该使用该表中的 entrie_id 从 nv_entries 和 nv_images 中获取内容。

表格如下所示:

nv_entries:
id - title - year - etc..

nv_tags:
id - entrieid - tag

nv_images:
id - entrieid - source

查询失败:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的“GROUP BY nv_entries JOIN ON nv_entries.id = nv_tags.entrie_id AND nv_tags.tag IN”附近使用正确的语法

在下面的脚本中生成的查询如下所示:

GROUP BY nv_entries JOIN ON nv_entries.id = nv_tags.entrie_id AND nv_tags.tag IN ('tree','green') HAVING COUNT(DISTINCT nv_tags.tag) = 2

这是脚本(部分):

for ($i = 0; $i < count($tags); $i++) {
    $tags[$i] = trim($tags[$i]);
}

$query = "GROUP BY nv_entries JOIN ON nv_entries.id = nv_tags.entrie_id AND nv_tags.tag IN (";
// add with following comma
for ($i = 0; $i < count($tags)-1; $i++) {
    $query .= "'".$tags[$i]."',";
}
// add last without a comma
$query .= "'".$tags[count($tags)-1]."'";

$query .= ") HAVING COUNT(DISTINCT nv_tags.tag) = ".count($tags);

echo $query;

$entries = execSelect($query);


echo "<pre>";
print_r($entries);
echo "</pre>";
4

1 回答 1

2

Your query is malformed!

A draft cold be the following:

$sql ="
select e.*
from nv_entries e
JOIN nv_tags t on t.entrie_id = e.id
where t.tag in ( $tag_list )
group  by e.id
having count(t.id) = $num_tags ";

ADD join with images

This query will return 1 line per image, using LEFT avoid missing line if missing image.

$sql ="
select e.*, i.*, group_concat( t.tag separator ',') as tag_list
from nv_entries e
JOIN nv_tags t on t.entrie_id = e.id
LEFT JOIN nv_images i on i.entrie_id = e.id

where t.tag in ( $tag_list )
group  by e.id
having count(t.id) = $num_tags ";
于 2012-07-02T19:29:35.790 回答