PHP&;MySQL多个类别中的一个项目
我尝试从PHP&;MySQL多个类别中的一个项目,php,mysql,database,Php,Mysql,Database,我尝试从stores表中选择可以属于多个类别的所有项目 SELECT s.name FROM store_category AS sc LEFT JOIN stores AS s ON sc.store_id = c.id WHERE sc.category_id = 2; 这是我的表格结构: 存储表 id name id name store_id category_id 类别表格 id name id name store_id category_id store\u类别表格
stores
表中选择可以属于多个类别的所有项目
SELECT s.name
FROM store_category AS sc
LEFT JOIN stores AS s ON sc.store_id = c.id
WHERE sc.category_id = 2;
这是我的表格结构:
存储
表
id
name
id
name
store_id
category_id
类别
表格
id
name
id
name
store_id
category_id
store\u类别
表格
id
name
id
name
store_id
category_id
我尝试了以下查询,但它没有按我所希望的那样工作:
SELECT * FROM (stores JOIN store_category ON stores.id = store_category.store_id) JOIN categories ON store_category.category_id = categories.id
我想做的是:
选择在store\u category
*添加为伪代码
我该怎么做
PS:填充表的示例
`stores`
id | name
1 | amazon
1 | aliexpress
`categories`
id | name
1 | smartphones
2 | fashion
`store_category`
store_id | category_id
1 | 1
1 | 2
2 | 1
2 | 2
从这个表设计中,我想表示两个商店都包含在两个类别中如果您想选择属于名称为“some_category”类别的所有商店,那么您可以尝试以下方法:
SELECT aa.id, aa.name
FROM stores AS aa
INNER JOIN store_category AS bb
ON aa.id = bb.store_id
INNER JOIN category AS cc
ON bb.category_id = cc.id
WHERE cc.name = 'some_category';
SELECT s.name
FROM store_category AS sc
LEFT JOIN stores AS s ON sc.store_id = c.id
WHERE sc.category_id = 2;
您只需执行以下操作:
SELECT table1.*, table2.*, table3.* from table1, table2, table3
where table1.id=table2.id and table2.id=table3.id and
table3.id=table1.id;
在这个示例中,我首先获得类别名称的所有行,然后加入商店。因此,您不必查询具有这些类别的所有商店
SELECT s.name
FROM store_category AS sc
LEFT JOIN stores AS s ON sc.store_id = c.id
WHERE sc.category_id = 2;
这不是测试。如果它对您有效,请现在告诉我。您能再解释一下您想要什么吗?我想显示一个categorygroup中的所有商店,您有任何类型的php脚本吗?例如,设置数据库连接并发出某种sql查询。或者你想要一个完整的脚本?我有脚本和数据库连接。我只是无法根据该条件选择查询。它不起作用。让我再次解释一下:我想从
stores
where category\u id fromstore\u category
=$category\u id php变量中选择id和name,也可以作为store\u id请参见我的更新问题和表设计示例我已经编辑了我的anwser。所以很容易找到所有有分类的商店。在我的样本中,我使用cat_id 2感谢您。这就是我要找的。