如何通过条件查询从三个表中提取特定信息,形成长尾关键词?
- 内容介绍
- 文章标签
- 相关推荐
本文共计448个文字,预计阅读时间需要2分钟。
在MySQL数据库中,如何在一个查询中选出3个表并显示所有产品的价格,以下是结构:
sqlSELECT product_priceFROM table1JOIN table2 ON table1.product_id=table2.product_idJOIN table3 ON table1.product_id=table3.product_id;
如何在一个查询中选择3个表并显示所有产品的价格下面是我的数据库结构(MySql)类别-------------------------categor如何在一个查询中选择3个表并显示所有产品的价格
下面是我的数据库结构(MySql)
类别
-------------------------
category_id | parent_id |
-------------------------
1 | 0
2 | 1
3 | 1
4 | 1
5 | 2
6 | 3
products_to_categories
-------------------------
product_id | category_id|
-------------------------
54 | 0
55 | 2
56 | 2
57 | 2
58 | 3
59 | 3
60 | 4
制品
-------------------------
product_id | price |
-------------------------
54 | 10.50
55 | 11.20
56 | 1.00
57 | 22.20
58 | 32.0
59 | 32.0
60 | 22.0
以下是我的情况;
1. table categories : parent_id 1
(result : 2,3,4)
2. table products_to_categories : category_id result categories(result : 2,3,4)
(result : 55,56,57,58,59,60)
3. table products : inner join or left join table product to display price where product_id result products_to_categories(result : 55,56,57,58,59,60)
最终产出
55 - 11.20
56 - 1.00
57 - 22.20
58 - 32.0
59 - 32.0
60 - 22.0
在我发布这个问题之前,这是我之前的查询(我坚持如何进入条件2)
$sql_all mysql_query("SELECT cat.parent_id,cat.category_id FROM categories cat WHERE cat.parent_id1 ");
while($row mysql_fetch_array($sql_all)) {
echo $row[categories_id].;
}
谢谢.
解决方法:
试试这个,
SELECT c.*
FROM categories a
INNER JOIN products_to_categories b
ON a.category_id b.category_id
INNER JOIN products c
ON b.product_id c.product_id
WHERE a.parent_id 1
这将显示parent_id 1的products表中的所有记录.
标签php,mysql,sql
来源 codeday.me/bug/20190626/1288622.html
本文共计448个文字,预计阅读时间需要2分钟。
在MySQL数据库中,如何在一个查询中选出3个表并显示所有产品的价格,以下是结构:
sqlSELECT product_priceFROM table1JOIN table2 ON table1.product_id=table2.product_idJOIN table3 ON table1.product_id=table3.product_id;
如何在一个查询中选择3个表并显示所有产品的价格下面是我的数据库结构(MySql)类别-------------------------categor如何在一个查询中选择3个表并显示所有产品的价格
下面是我的数据库结构(MySql)
类别
-------------------------
category_id | parent_id |
-------------------------
1 | 0
2 | 1
3 | 1
4 | 1
5 | 2
6 | 3
products_to_categories
-------------------------
product_id | category_id|
-------------------------
54 | 0
55 | 2
56 | 2
57 | 2
58 | 3
59 | 3
60 | 4
制品
-------------------------
product_id | price |
-------------------------
54 | 10.50
55 | 11.20
56 | 1.00
57 | 22.20
58 | 32.0
59 | 32.0
60 | 22.0
以下是我的情况;
1. table categories : parent_id 1
(result : 2,3,4)
2. table products_to_categories : category_id result categories(result : 2,3,4)
(result : 55,56,57,58,59,60)
3. table products : inner join or left join table product to display price where product_id result products_to_categories(result : 55,56,57,58,59,60)
最终产出
55 - 11.20
56 - 1.00
57 - 22.20
58 - 32.0
59 - 32.0
60 - 22.0
在我发布这个问题之前,这是我之前的查询(我坚持如何进入条件2)
$sql_all mysql_query("SELECT cat.parent_id,cat.category_id FROM categories cat WHERE cat.parent_id1 ");
while($row mysql_fetch_array($sql_all)) {
echo $row[categories_id].;
}
谢谢.
解决方法:
试试这个,
SELECT c.*
FROM categories a
INNER JOIN products_to_categories b
ON a.category_id b.category_id
INNER JOIN products c
ON b.product_id c.product_id
WHERE a.parent_id 1
这将显示parent_id 1的products表中的所有记录.
标签php,mysql,sql
来源 codeday.me/bug/20190626/1288622.html

