-1

I'm wondering if anyone can help me to solve the problem that I meet. :) I have a table with articles, named article and also I have a table commonarticletechnique where I save all the techniques that can be used by reporters to compose an article. Also I have a table articletechnique where I have references between articles and common articles techniques. Sorry for my english, I'm not a native english speaker. So my situation looks like:

Article(Id,Many other fields...)

CommonArticleTechnique(Id, Name, Description)

ArticleTechnique(Id, FkArticleId, FkCommonArticleTechnique)

For example I can have in tables things like:

  • in article an article with id 282;
  • in commonarticletechnique all kind of techniques used by users to write an article;
  • in articletechnique i can have

1, 282, 12

2, 282, 13

3, 282, 14

So, with these records, I can say that article with id 282 have 3 techniques of conception.

In UI I have a section where users can search in my database and they can check none, one, ore more article techniques. So if I want to find an article that is based on techniques 12, 13, 14 I have to write an mysql query to get all the articles that are matching exactly with my techniques.

In my example if user select 12, 13 techniques, mysql shouldn't return the article 282. In this case the problem that rises up is bounded by the intersection of two mysql subsets. One of them is from a collection extracted from a table and the other subset is from UI, from what's the selection of users.

Please tell me if know how to write the mysql query and also it is very helpfully to not implement a stored procedure.

Thanks for all, Aryliah.

4

1 回答 1

0

我想在同一篇文章中不能有两个具有相同技术的条目(例如两次 282、12)。这也意味着您的文章技术表中不需要Id PK。

反正。

您的查询应该是这样的:

SELECT t1.FkArticleId
FROM ArticleTechnique t1
WHERE t1.fkArticleId IN (SELECT t2.FkArticleId
                         FROM ArticleTechnique t2
                         WHERE t2.FkCommonArticleTechnique IN (12, 13))
GROUP BY t1.FkArticleId
HAVING COUNT(*) = 2;

当然,xinHAVING COUNT(*) = x必须等于所选技术的数量(在这种情况下,12、13 是...... 2 个技术)

于 2013-05-28T13:32:51.530 回答