dongle0396 2010-06-29 00:43
浏览 12
已采纳

如何查询多对多

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.

  • 写回答

3条回答 默认 最新

  • dongzhang1875 2010-06-29 03:45
    关注

    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.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒