私はこのようなテーブルを持っています:
+----+--------------------------------+
| id | slug |
+----+--------------------------------+
| 1 | gift |
| 1 | psychological-manipulation |
| 1 | christmas |
| 1 | giving |
| 1 | the-town-santa-forgot |
| 1 | santa-claus |
| 1 | mp3 |
| 1 | christmas |
| 2 | entertainment-culture |
| 2 | christmas |
| 2 | culture |
| 2 | literature |
| 2 | christmas-music |
| 2 | christmas-window |
| 2 | broadcasting-nec |
| 2 | how-the-grinch-stole-christmas |
| 2 | the-polar-express |
| 2 | banker |
| 2 | christmas |
| 2 | potter |
| 2 | christmas-eve |
| 2 | bailey |
| 2 | its-a-wonderful-life |
| 2 | the-polar-express |
| 2 | disney |
| 2 | tim-burton |
| 2 | a-christmas-carol |
| 2 | the-nightmare-before-christmas |
| 2 | chuck-jones |
+----+--------------------------------+
スラッグのリストの少なくとも2つが特定のIDに一致するテーブルから一意のIDを取得したいと思います。
たとえば、次のスラッグ値があるとします。
そのうちの少なくとも2つに一致するレコードを持つすべての一意のIDが必要です。
つまり、ギフトとクリスマスのナメクジ、またはギフトとクリスマスのナメクジ、またはギフトとギフトのナメクジの両方を持っていたIDのみ...
distinct
修飾子を使用して、IDごとの異なるスラッグの数を数えることができます。
SELECT id
FROM mytable
WHERE slug IN ('gift', 'christmass', 'giving')
GROUP BY id
HAVING COUNT(DISTINCT slug) >= 2
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加