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