MYSQL 从多个表中提取多个值
MYSQL Pulling multiple values from multiple tables
请原谅我的标题选择不当,我不知道该用什么来标记这个问题,但相信我可以解释。
我在产品 table 和商店 table 之间建立了多对多关系。
table name: products
+-------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(200) | NO | | NULL | |
| sku | varchar(10) | NO | | NULL | |
| units | int(5) | YES | | NULL | |
+-------+------------------+------+-----+---------+----------------+
table name: stores
+----------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| number | int(5) | NO | | NULL | |
| location | varchar(50) | NO | | NULL | |
| phone | varchar(10) | YES | | NULL | |
+----------+------------------+------+-----+---------+----------------+
table name: products_stores
+-------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+-------+
| pID | int(10) unsigned | NO | PRI | NULL | |
| sID | int(10) unsigned | NO | PRI | NULL | |
+-------+------------------+------+-----+---------+-------+
我是 运行 查询以显示其 ID = 1
的商店中可用的所有产品
SELECT p.name, p.sku
FROM products p
INNER JOIN products_stores ps
ON p.id = ps.pID
WHERE ps.sID = 1;
我对这个查询没问题,得到了我想要的结果。但是,在我的显示结果中,我还想显示每个结果行的商店位置值。我以为我可以在我的 FROM 语句中包含逗号分隔,例如:
SELECT p.name, p.sku, s.location
FROM products p, stores s
INNER JOIN products_stores ps
ON p.id = ps.pID
WHERE ps.sID = 1;
但是这不起作用。我相信答案很简单,我会继续研究解决方案。我想我会把它扔掉,希望有人能引导我朝着正确的方向前进。
谢谢。
SELECT
p.name,
p.sku,
s.location
FROM
products p,
stores s,
products_stores ps
WHERE p.id = ps.pID
AND s.sID = ps.sID
AND ps.sID = 1 ;
使用显式 JOIN,隐式 JOIN 已弃用(并且不太清楚)
SELECT p.name, p.sku ,s.location
FROM products p
INNER JOIN products_stores ps
ON p.id = ps.pID
INNER JOIN stores s
ON s.ID=ps.sID
WHERE ps.sID = 1;
请原谅我的标题选择不当,我不知道该用什么来标记这个问题,但相信我可以解释。
我在产品 table 和商店 table 之间建立了多对多关系。
table name: products
+-------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(200) | NO | | NULL | |
| sku | varchar(10) | NO | | NULL | |
| units | int(5) | YES | | NULL | |
+-------+------------------+------+-----+---------+----------------+
table name: stores
+----------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| number | int(5) | NO | | NULL | |
| location | varchar(50) | NO | | NULL | |
| phone | varchar(10) | YES | | NULL | |
+----------+------------------+------+-----+---------+----------------+
table name: products_stores
+-------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+-------+
| pID | int(10) unsigned | NO | PRI | NULL | |
| sID | int(10) unsigned | NO | PRI | NULL | |
+-------+------------------+------+-----+---------+-------+
我是 运行 查询以显示其 ID = 1
的商店中可用的所有产品SELECT p.name, p.sku
FROM products p
INNER JOIN products_stores ps
ON p.id = ps.pID
WHERE ps.sID = 1;
我对这个查询没问题,得到了我想要的结果。但是,在我的显示结果中,我还想显示每个结果行的商店位置值。我以为我可以在我的 FROM 语句中包含逗号分隔,例如:
SELECT p.name, p.sku, s.location
FROM products p, stores s
INNER JOIN products_stores ps
ON p.id = ps.pID
WHERE ps.sID = 1;
但是这不起作用。我相信答案很简单,我会继续研究解决方案。我想我会把它扔掉,希望有人能引导我朝着正确的方向前进。
谢谢。
SELECT
p.name,
p.sku,
s.location
FROM
products p,
stores s,
products_stores ps
WHERE p.id = ps.pID
AND s.sID = ps.sID
AND ps.sID = 1 ;
使用显式 JOIN,隐式 JOIN 已弃用(并且不太清楚)
SELECT p.name, p.sku ,s.location
FROM products p
INNER JOIN products_stores ps
ON p.id = ps.pID
INNER JOIN stores s
ON s.ID=ps.sID
WHERE ps.sID = 1;