如何查询多对多

发布于 2024-09-07 05:17:50 字数 801 浏览 1 评论 0原文

我正在尝试对这些字段进行多对多查询。我想得到:

1)某个类别中的所有帖子

2)某个帖子中的所有类别

3)某个类别中具有特定 id 的所有帖子

    posts
+-------------+--------------+
| id          | int(11)      |
| title       | varchar(255) |
| body        | text         |
| parent_id   | int(11)      |
| category_id | int(11)      |
+-------------+--------------+
    post_categories 
+----------+--------------+
| id       | int(11)      |
| category | varchar(255) |
+----------+--------------+
    post_category_bridge
+-------------+-------------+
| id          | int(11)     |
| post_id     | int(11) |
| category_id | int(11) |
+-------------+-------------+

我担心的一件事是我正在 MySQL DB 上使用 PHP 的 PDO 进行开发,但我将在发布当天将该站点转移到 SQL Server。我知道 MySQL 和 SQL Server 之间存在差异。 POD 会处理这些差异吗?还是我需要重写这些查询。

提前致谢。

I'm trying to do a many to many query on these fields. I'd like to get:

1) all the posts that are in a category

2) all the categories that are in a post

3) all the posts that are in a category that have a specific id

    posts
+-------------+--------------+
| id          | int(11)      |
| title       | varchar(255) |
| body        | text         |
| parent_id   | int(11)      |
| category_id | int(11)      |
+-------------+--------------+
    post_categories 
+----------+--------------+
| id       | int(11)      |
| category | varchar(255) |
+----------+--------------+
    post_category_bridge
+-------------+-------------+
| id          | int(11)     |
| post_id     | int(11) |
| category_id | int(11) |
+-------------+-------------+

One thing I'm worried about is that I'm using PHP's PDO on a MySQL DB for development but I will be transferring the site to an SQL Server on launch day. I know there are differences between MySQL and SQL Server. Will POD take care of those differences or will I need to re-write these queries.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

少钕鈤記 2024-09-14 05:17:50

我使用详细联接语法来更清楚地了解表之间的关系。

1) 类别中的所有帖子

给定类别名称,您需要连接所有三个表。

select p.*
  from post_category c
    join post_category_bridge b on c.id = b.category_id
    join posts p                on p.id = b.post_id
  where c.category = ?

2) 帖子中的所有类别

给定帖子 ID,您只需连接桥表和类别表即可。

select c.*
  from post_category_bridge b
    join post_category c        on c.id = b.category_id
  where b.post_id = ?

3) 具有特定 id 的类别中的所有帖子

我认为您的意思是通过此处的 category.id 查找帖子(而不是 category.id)。 name) 与 (1) 类似,但不需要加入类别表,因为您已经知道 id;您只需要加入桥接表和帖子表即可。

select p.*
  from post_category_bridge b
    join posts                  on p.id = b.post_id
  where b.category_id = ?

我将在发布当天将网站转移到 SQL Server...POD 会处理这些差异还是我需要重写这些查询。

这取决于最终的查询你的系统。如果您正在编写自己的 SQL,那么在开发过程中是否使用 MySQL 特有的功能或语法将会很重要。我强烈建议早于发布日之前在 SQL Server 上进行测试,否则您可能会发现发布被推迟了一段时间。为此,您可以下载免费的评估版目的。

评论中提到的值得重复的要点:

  • 正如@freddy提到的,你没有'不需要 posts.category_id 字段。在多对多关系中,桥(又名“连接”、“连接”、“地图”、“链接”等)表将帖子链接到多个类别 - 帖子 上的单个字段如果仅允许一个类别,则将使用表。
  • 正如@JamieWong提到的,您应该保持表键和外键之间的类型一致,例如如果posts。 idint(11),那么 post_category_bridge.post_id 也应该是 int(11)。大多数(所有?)强制外键约束的数据库都需要这个(包括 MySQL)。为什么?如果可以有 4294967295 个帖子(由 4 字节 int 支持),则仅支持链接到 255 个帖子(由 1 字节 tinyint 支持)的桥接表没有什么意义。 code>)
  • 当您这样做时...不妨将 ID(以及这些 ID 的 FK)无符号

I'm using the verbose join syntax to be more clear on how the tables are related.

1) all the posts that are in a category

Given the category name, you need to join all three tables.

select p.*
  from post_category c
    join post_category_bridge b on c.id = b.category_id
    join posts p                on p.id = b.post_id
  where c.category = ?

2) all the categories that are in a post

Given the post id, you only need to join the bridge and category tables.

select c.*
  from post_category_bridge b
    join post_category c        on c.id = b.category_id
  where b.post_id = ?

3) all the posts that are in a category that have a specific id

I think you mean looking up posts by category.id here (as opposed to category.name) which is similar to (1) but does not need to join on the category table, as you already know the id; you only need to join the bridge and post tables.

select p.*
  from post_category_bridge b
    join posts                  on p.id = b.post_id
  where b.category_id = ?

I will be transferring the site to an SQL Server on launch day...Will POD take care of those differences or will I need to re-write these queries.

This depends on the queries that end up in your system. If you're writing your own SQL then it will matter if you use features or syntax unique to MySQL during development. I highly recommend testing on SQL Server Long before launch day or you may find launch postponed for a while. You can download a free evaluation version for just this purpose.

Points mentioned in the comments that bear repeating:

  • as @freddy mentions, you don't need the posts.category_id field. In many-to-many relationships, the bridge (aka 'junction', 'join', 'map', 'link', etc) table links posts to multiple categories - a single field on the posts table would be used if only one category were allowed.
  • as @JamieWong mentions, you should keep types consistent between table keys and foreign keys, e.g. if posts.id is int(11), then post_category_bridge.post_id should also be int(11). Most (all?) databases that enforce foreign key constraints will require this (including MySQL). Why? If a there can be 4294967295 posts (as supported by the 4 byte int) there's little point in a bridge table that only supports linking to 255 posts (as supported by the 1 byte tinyint)
  • While your at it... might as well make the IDs (and FKs to those IDs) unsigned.
请持续率性 2024-09-14 05:17:50

这有帮助吗?

1:

select p.* from posts p, post_categories c
where p.category_id=c.id and category='something'

2:

select c.* from post_categories, posts p
where p.category_id=c.id and post_id=something

3:

select p.* from post_categories, posts p
where p.category_id=c.id and post_id=something and category='something'

Does this help?

1:

select p.* from posts p, post_categories c
where p.category_id=c.id and category='something'

2:

select c.* from post_categories, posts p
where p.category_id=c.id and post_id=something

3:

select p.* from post_categories, posts p
where p.category_id=c.id and post_id=something and category='something'
始终不够 2024-09-14 05:17:50

select a.*  from posts a, post_categories b, post_category_bridge c 
WHERE 
     b.category="mycat" AND 
     b.id=c.category_id AND 
     a.id = c.post_id;

select b.category from posts a, post_categories b, post_category_bridge c,
WHERE
    c.post_id='PostID' AND
    b.id = c.category_id;


select a.*  from posts a, post_categories b, post_category_bridge c 
WHERE 
     b.category="mycat" AND 
     c.category_id = b.id AND 
     c.post_id = 'MyID' AND
     a.id = c.post_id;

select a.*  from posts a, post_categories b, post_category_bridge c 
WHERE 
     b.category="mycat" AND 
     b.id=c.category_id AND 
     a.id = c.post_id;

select b.category from posts a, post_categories b, post_category_bridge c,
WHERE
    c.post_id='PostID' AND
    b.id = c.category_id;


select a.*  from posts a, post_categories b, post_category_bridge c 
WHERE 
     b.category="mycat" AND 
     c.category_id = b.id AND 
     c.post_id = 'MyID' AND
     a.id = c.post_id;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文