4

video:

v_id, 
v_name

property:

p_id,
p_name

property_video:

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

I want to search muti-cat eg:

Given some p_id (property id) I want to search match those id video

need all p_id in property_video and same pv_v_id

then mysql wrote:

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;

i know this part in(12,15) must change to "and" but I do not know how to make it work.

Barmar
  • 741,623
  • 53
  • 500
  • 612

3 Answers3

1

Here, we will join property_video and video on the column that indicates video ID. The where condition allows us to limit the result only to videos that have the properties contained in the IN clause.

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.

davesnitty
  • 1,800
  • 14
  • 11
0

i think you need to use innerJoin, Try this out.

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;
d3bug3r
  • 2,492
  • 3
  • 34
  • 74
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);

Assuming that you are looking for videos with property IDs 12 or 15.

Russell Gutierrez
  • 1,372
  • 8
  • 19