4

视频:

v_id, 
v_name

财产:

p_id,
p_name

财产视频:

pv_id,
pv_v_id, <- video id
pv_p_id, <- property id

我想搜索 muti-cat 例如:

给定一些 p_id(属性 id)我想搜索匹配那些 id 视频

需要 property_video 中的所有 p_id 和相同的 pv_v_id

然后mysql写道:

SELECT
  a.*, 
  b.* 
FROM 
  video as a, 
  property_video as b 
WHERE
  a.v_id = b.pv_v_id 
  and b.pv_p_id in(12,15) 
GROUP BY a.v_id;

我知道这部分 in(12,15)必须改为“和”,但我不知道如何使它工作。

4

3 回答 3

1

在这里,我们将property_video 和video 加入到表示视频ID 的列上。where 条件允许我们将结果限制为仅具有包含在 IN 子句中的属性的视频。

SELECT v.v_id
FROM property_video pv
    JOIN video v
    ON pv.pv_v_id = v.v_id
WHERE pv.pv_p_id IN (12,15)
GROUP BY v.v_id
HAVING count(distinct pv.pv_p_id) = 2

By grouping by the video ID and then only keeping the videos that have 2 distinct properties (i.e. must have both, not just one), you achieve the multi-category requirement. This assumes that your method to build the syntax of the query gives you the ability to adjust the number in the HAVING clause to match the number of distinct properties in your intended filter.

于 2012-10-18T03:32:33.447 回答
0

我认为你需要使用 innerJoin,试试这个。

SELECT
  a.*, 
  b.* 
FROM 
  video as a, 
InnerJoin property_video as b
WHERE a.v_id = b.pv_p_id
and b.pv_p_id in(12,15) 
GROUP BY a.v_id;
于 2012-10-18T02:20:44.590 回答
0
select a.*, b.*, c.* from video as a, property_video as b, property as c 
where a.v_id = b.pv_v_id and c.p_id = b.pv_p_id and c.p_id in (12,15);

假设您正在寻找属性 ID为1215的视频。

于 2012-10-18T02:21:41.487 回答