tags:

views:

92

answers:

1

I must be honest and tell you that I am not good at database queries and this question is probably quite simple.

I have three tables

Post
    ID
    entry
Category
    ID
    name
CategoryBinding
    ID
    postID
    categoryID

My normal query is to get all posts with the categories it is put into

SELECT * FROM `Post` AS `p` 
LEFT JOIN `CategoryBinding` AS `cb` ON p.ID = cb.postID 
LEFT JOIN `Category` AS `c` ON cb.categoryID = c.ID

The returned query for this is something like:

ID    entry    ID    name    ID    postID    categoryID
1     entry1    1     php      1      1            1
1     entry1    2     asp      1      1            2

2     entry2    1     php      1      2            1

3     entry3    null  null    null    null        null

Now I want to get all posts that belongs to a certain category ID with all the categories the post is put into.
I.E I want to get the same things as in the first query BUT only the posts that belong to a certain category. Now I only want to get the posts that belongs the category asp. That is

ID    entry    ID    name    ID    postID    categoryID
1     entry1    1     php      1      1            1
1     entry1    2     asp      1      1            2

Do you know how I can do this?

I will be very thankful if someone helps me since this is more like a "Do the work for me" question.

+3  A: 
SELECT * 
FROM `Post` AS `p` 
LEFT JOIN `CategoryBinding` AS `cb` ON p.ID = cb.postID 
LEFT JOIN `Category` AS `c` ON cb.categoryID = c.ID
INNER JOIN `Post` AS `p2` ON p.id = p2.id
WHERE p.id in
(
    SELECT p2.id
    FROM `Post` as `p2`
    LEFT JOIN `CategoryBinding` AS `cb` ON p2.ID = cb.postID 
    LEFT JOIN `Category` AS `c` ON cb.categoryID = c.ID
    WHERE c.id = @SomeCategory
)

Alright, final shot.

This will return duplicate rows, just add a group by to whatever you want.

Brandon
I changed my question and added expected output.
unkown
The query you wrote will only give me1 entry1 2 asp 1 1 2
unkown
Alright, I updated the query. Give this one a shot.
Brandon
I get exactly the same result as with the other query you wrote. But I think you are on the right path. I was thinking something similar but with one more categoryBinding instead of one more Post join, but since my mysql knowledge is limited I have not been able to do it.
unkown
Alright, final try >_>
Brandon
Thank you it worked. I wanted to add an inner query, but did not know that it was possible. You do however have an error in the inner query it should be p2.ID and not p.ID on the third line from the bottom.
unkown
Right you are. I'll fix that.
Brandon
I have to reopen this question since it works in phpmyadmin but not with Zend_DB PDO or mysqli adopter. I get this error message: "Operand should contain 1 column(s)" with the mysqli adopter.
unkown
No clue, but that doesn't seem like a problem related to the original question since the original question was just about writing an sql query. This seems specific to Zend. (Which should have been mentioned in the original post by the way). :P
Brandon
Ok, I will then start a new post? and hope that it dose not count as double posting.
unkown
I believe it doesn't, as your issue is now with Zend and not query writing.
Brandon