dongzanghong4379 2018-01-15 11:20
浏览 21

可以使用PHP / MySQL列出多个类别中的相同项目吗?

I have a website which lists products in various categories and subcategories - fairly standard stuff. However, I now need to list the same product in multiple categories - how can I do this?

I'm using the code:

"SELECT * FROM ProductTable WHERE Category = 1"

I can get it to work by entering multiple numbers in the Category table i.e."1 2 3" and using the code:

"SELECT * FROM ProductTable WHERE Category LIKE 1"

But this also returns products from category 10 and 11 (I have 20 categories).

I may be approaching this from the wrong angle but is there a way to make the code work?

  • 写回答

3条回答 默认 最新

  • douqiu9529 2018-01-15 11:31
    关注

    Since you no longer have a one-to-many relationship between categories and products you will need a new table to give the desired many-to-many relationship. Something like:

    product_categories
    ------------------
    category_id INT
    product_id INT
    

    Primary key: (category_id, product_id)

    This will allow a product to belong to multiple categories in a normalized way instead of treating the Category field as a list of ids.

    评论

报告相同问题?

悬赏问题

  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本