gpt4 book ai didi

php - 按类别列出项目

转载 作者:行者123 更新时间:2023-11-29 02:37:54 25 4
gpt4 key购买 nike

我有 3 个表:

类别无关紧要

item2cat:itemID|catID

项目:id|name|desc

我想列出给定类别中的项目,但我不知道如何以简单的方式做到这一点。 (使用 PHP 和 MySQL)

我需要这个表结构,因为我想要一个项目有多个类别。

最佳答案

您可能想要 join item 表上的 item2cat 表:

SELECT
item.id
, item.name
, item.desc
FROM item
INNER JOIN item2cat ON item.id = item2cat.itemID
WHERE item2cat.catID = [category_id]

或者例如

SELECT
item.id
, item.name
, item.desc
, category.id
, category.name
FROM item
INNER JOIN item2cat ON item.id = item2cat.itemID
INNER JOIN category ON item2cat.catID = category.id
WHERE category.id IN ( [category_id], [category_id], [category_id])

更新
如果您像这样更改表 ID:

item (itemId, name, desc)
item2cat (itemId, categoryId)
category (categoryId, name, etc)

您可以像这样重写第一个查询:

SELECT
item.itemId
, item.name
, item.desc
FROM item
INNER JOIN item2cat USING( itemId )
WHERE item2cat.categoryId = [category_id]

另一个优点是每个表的 id 列现在是明确的。

关于php - 按类别列出项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2217030/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com