English 中文(简体)
很多人到表K甄选问题
原标题:Many to Many Table SQL Selection Problem
  • 时间:2010-08-31 09:23:00
  •  标签:
  • php
  • sql
  • mysql

我有两个相关的表格,为这一问题的目的,这些表格将称为<编码>posts和tags。 <代码>tags载有一套独特的标签,当服务器碰到一个尚未进入系统的新标签时,添加这些标签。 参赛者只有一人。

<代码>后 可设多个标签,<代码>tags可有一个以上员额作为参考。 为了重新处理这些参考文献,我创建了一个表格,在这两个表格之间坐下来,即<编码>序号。 <代码>posttags 。 这就是维持多少到许多关系的方法。

现在,这个问题已经解决。 我需要能够根据对口挑选职位。 当只有一个政党去寻找时,这只是一个简单的举动,但我对如何处理多个方面感到损失。 例如,我需要能够搜索数据库,并取得清单中所有标的(例如“php、我的sql、 sql”)的结果,而不必使用文件库或任何其他低性能选择。

我不知道如何做到这一点。 谁能向我指出正确的方向?

感谢!

最佳回答

你们可以采用以下办法,3个是要求方的数量。

SELECT PostId
FROM posttags pt, tags t
WHERE p.Id = pt.PostId
  AND t.Id = pt.TagId
  AND tagname In ( php ,  mysql ,  sql )
group by PostId
having count(*) = 3

或你可以把要求参赛的各方合在一起,以便使用:

Select * from Posts p,
(  
 SELECT PostId, GROUP_CONCAT(tagname ORDER BY tag_name DESC SEPARATOR    ) Tags
 FROM posttags pt, tags t
 WHERE p.Id = pt.PostId
   AND t.Id = pt.TagId
 GROUP BY PostId

) t WHERE t.PostId = p.Id AND t.Tags = mysql php sql

但你们需要打上你们的标签。

问题回答

如果你想选择某一组别中的“all> 标签”职位,你可以采用“计票”战略:

SELECT
  p.*
FROM tags t
LEFT JOIN posttags pt
  ON pt.tag_id = t.tag_id
LEFT JOIN posts p
  ON p.post_id = pt.post_id
WHERE
  tags.tag IN ( php ,  mysql ,  sql )
GROUP BY p.post_id
HAVING COUNT(tags.tag_id) = 3;

当然,如果你改变该套标准,“3”号应改为该套内容的数量。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签