如何使用PHP在条件查询中同时选取三个相关联的表?

2026-06-10 21:103阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计491个文字,预计阅读时间需要2分钟。

如何使用PHP在条件查询中同时选取三个相关联的表?

如何在查询中选取3个表并显示所有产品的价格?下面是我的数据库结构(MySQL):

sqlCREATE TABLE `categor` ( `id` int NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, PRIMARY KEY (`id`));

CREATE TABLE `product` ( `id` int NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, `price` decimal(10,2) NOT NULL, `category_id` int NOT NULL, PRIMARY KEY (`id`), KEY `category_id` (`category_id`), CONSTRAINT `product_ibfk_1` FOREIGN KEY (`category_id`) REFERENCES `categor` (`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

如何使用PHP在条件查询中同时选取三个相关联的表?

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

本文共计491个文字,预计阅读时间需要2分钟。

如何使用PHP在条件查询中同时选取三个相关联的表?

如何在查询中选取3个表并显示所有产品的价格?下面是我的数据库结构(MySQL):

sqlCREATE TABLE `categor` ( `id` int NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, PRIMARY KEY (`id`));

CREATE TABLE `product` ( `id` int NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, `price` decimal(10,2) NOT NULL, `category_id` int NOT NULL, PRIMARY KEY (`id`), KEY `category_id` (`category_id`), CONSTRAINT `product_ibfk_1` FOREIGN KEY (`category_id`) REFERENCES `categor` (`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

如何使用PHP在条件查询中同时选取三个相关联的表?

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