PHP/MySQL:检查某物是否“属于”还有什么吗?

发布于 2024-08-31 00:04:18 字数 378 浏览 5 评论 0原文

我有两个表:商店和物品。关系是: Stores 1---* Items

在 PHP/MySQL 中,检查特定项目是否属于特定商店的最佳(最快/最简单)方法是什么。

换句话说,例如:

$store_id = 1;
$item_id = 12;

我想检查商品 12 是否属于商店 1(而不是其他商店)。

我通常对同时匹配 store_id 和 item_id 的项目进行选择,并将结果限制为 1。然后检查 mysql_num_rows 返回了多少行(0 或 1)。有更好的办法吗?

更新:

两个表都有一个“id”列。 Items 表有一个“store_id”列。

I have two tables: Stores and Items. The relationship is: Stores 1---* Items

In PHP/MySQL what would be the best (fastest/simplest) way to check if a particular item belongs to a particular store.

In other words given for example:

$store_id = 1;
$item_id = 12;

I want to check if item 12 belongs to store 1 (and not some other store).

I usually do a select on Items matching both the store_id and item_id and limit the results to 1. Then check how many rows (0 or 1) were returned with mysql_num_rows. Is there a better way?

Update:

Both tables have an "id" column. The Items table has a "store_id" column.

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

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

发布评论

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

评论(4

葮薆情 2024-09-07 00:04:18
SELECT COUNT(*) AS count
FROM stores JOIN items USING(store_id)
WHERE item_id = 12
  AND store_id = 1

然后你会得到结果,并检查 count > 0 或不是。但是,如果我的数据库设计正确,那么您的数据库就会非常混乱。

根据您的描述,一件商品只能存在于一家商店。所以我对这里总体布局的猜测是这样的:

STORE            ITEM
-----            ----
store_id ---|    item_id
store_name  |--- store_id
...              item_name
                 ...

这是正确的吗?一件商品除了在一家商店之外就永远不可能存在?那么,如果它是一把螺丝刀,每个商店都需要不同的 item_id 来存放它?

更好的设计是:

STORE            STORE_ITEM         ITEM
-----            ----------         ----
store_id ------- store_id   |------ item_id
store_name       item_id ---|       item_name
...                                 ...

通过查询

SELECT COUNT(*)
FROM store JOIN store_item USING(store_id)
     JOIN item USING(item_id)
WHERE store_id = 1
  AND item_id = 12
SELECT COUNT(*) AS count
FROM stores JOIN items USING(store_id)
WHERE item_id = 12
  AND store_id = 1

Then you'd get the results, and check of count > 0 or not. However, if I'm getting your DB design right, then you have a very messed up database.

From what you describe, an item can only exist in one store. So my guess of the general layout here would be like this:

STORE            ITEM
-----            ----
store_id ---|    item_id
store_name  |--- store_id
...              item_name
                 ...

Is this correct? An item can never exist except in the one store? So if it's a screwdriver, every store would need a different item_id to hold it?

A better design would be:

STORE            STORE_ITEM         ITEM
-----            ----------         ----
store_id ------- store_id   |------ item_id
store_name       item_id ---|       item_name
...                                 ...

With a query of

SELECT COUNT(*)
FROM store JOIN store_item USING(store_id)
     JOIN item USING(item_id)
WHERE store_id = 1
  AND item_id = 12
能怎样 2024-09-07 00:04:18

两个表都有一个 id,Items 有一个 store_id

SELECT COUNT(*) FROM Items WHERE store_id = $store_id AND id = $item_id

Both tables have an id, Items has a store_id

SELECT COUNT(*) FROM Items WHERE store_id = $store_id AND id = $item_id
千纸鹤 2024-09-07 00:04:18
$r = mysql_query("select NULL from Item where storeID = '$store_id' and ItemID = '$item_id'");

if (mysql_fetch_row($r))
{
    it belongs...
}
$r = mysql_query("select NULL from Item where storeID = '$store_id' and ItemID = '$item_id'");

if (mysql_fetch_row($r))
{
    it belongs...
}
疏忽 2024-09-07 00:04:18

为了好玩,我将添加一个单行检查:

// if item belongs to store
if (current(mysql_fetch_array(mysql_query("SELECT COUNT(*) FROM Items WHERE store_id = $store_id AND id = $item_id"), MYSQL_NUM)))) {
    // it belongs!
}

For fun, I'll throw in a one-liner check:

// if item belongs to store
if (current(mysql_fetch_array(mysql_query("SELECT COUNT(*) FROM Items WHERE store_id = $store_id AND id = $item_id"), MYSQL_NUM)))) {
    // it belongs!
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文