Jump to content

MySQL Help - linking table query

Probably not a way around it...

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

MySQL Help - linking table query Probably not a way around it... Rate Topic: -----

#1 User is offline   Spode 

  • W.R. Private
  • Group: Members
  • Posts: 28
  • Joined: 13-November 06

Posted 07 January 2007 - 08:41 PM

Hi Guys,

Fairly simple scenario, but short of having to do some PHP work and then make a second query, here is the problem.

I have a "link table" as I like to call it, which has category_id,video_id as fields. The idea being so I can give a video more than one assocated category_id.

So I search the table, for all videos, where the category_id is X. However, I'd like to nest the categories. For instance, I want a video that's category is X AND X.

Am I going to have to do a query if the category is X OR X and then use PHP to ween out the ones that aren't in both? Or can I do this in SQL?

More to the point, which would be less resource hungry?
0

#2 User is offline   Catalyst 

  • Codesmith
  • Group: Administrators
  • Posts: 1,049
  • Joined: 04-April 06
  • Gender:Male
  • Location:San Diego

Posted 07 January 2007 - 09:50 PM

SQL is going to give you better performance. While this isn't the only way to do it I'd say what you want is to INTERSECT two queries, this gives you the results that are in both of them.

Generally it would by:

SELECT video_id FROM linktable WHERE category_id=12
INTERSECT
SELECT video_id FROM linktable WHERE category_id=19

But, MySQL doesn't support INTERSECT. Fortunately you can do the same thing using a self join:

SELECT A.video_id
FROM linktable A INNER JOIN linktable B
ON A.video_id=B.video_id
WHERE A.category_id=12 AND B.category_id= 19
0

#3 User is offline   Catalyst 

  • Codesmith
  • Group: Administrators
  • Posts: 1,049
  • Joined: 04-April 06
  • Gender:Male
  • Location:San Diego

Posted 07 January 2007 - 10:04 PM

Here's another way to do it that doesn't use INTERSECT and might be easier if you have more than 2 categories you're trying to match (I'll use 5 in this example)

SELECT video_id
FROM linktable
WHERE category_id IN (3,7,9,11,18) <-- Comma'd list of categories
group by video_id
HAVING count(video_id)=5 <-- Number of items in the comma'd list
0

#4 User is offline   Spode 

  • W.R. Private
  • Group: Members
  • Posts: 28
  • Joined: 13-November 06

Posted 08 January 2007 - 09:13 AM

Once again proving that my knowledge of SQL is definetely a weak point for me :)

I'll look in to those now, but I have a strong suspicion that should work nicely.
0

#5 User is offline   Ben Abrams 

  • The buddy system:never fails
  • Group: Administrators
  • Posts: 1,850
  • Joined: 04-April 06
  • Gender:Male

Posted 08 January 2007 - 09:36 AM

yeah they seem quality solutions, they should work perfectly

View PostSirkent, on 21 September 2007 - 04:26 AM, said:

<monty python high-pitched female voice>I DON'T LIKE SPAM!</monty python high-pitched female voice>
0

#6 User is offline   Spode 

  • W.R. Private
  • Group: Members
  • Posts: 28
  • Joined: 13-November 06

Posted 08 January 2007 - 12:09 PM

Yup, got it working. I had a bit of a search around some of the stuff in those queries and it really amazes me how powerful MySQL is.

However, what I did in the end, was basically what you suggested, GROUP BY, and then HAVING count(video_id) > x. Lovely :)
0

#7 User is offline   Catalyst 

  • Codesmith
  • Group: Administrators
  • Posts: 1,049
  • Joined: 04-April 06
  • Gender:Male
  • Location:San Diego

Posted 08 January 2007 - 02:24 PM

Great. That was definitely the more flexible of the two ways.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users