php – 在一个带有条件的查询中选择3个表
发布时间:2020-12-13 17:13:11 所属栏目:PHP教程 来源:网络整理
导读:如何在一个查询中选择3个表并显示所有产品的价格? 下面是我的数据库结构(MySql): 类别 +-------------+------------+ category_id | parent_id |+-------------+------------+ 1 | 0 2 | 1 3 | 1 4 | 1 5 | 2 6 | 3 products_to_categories +-------------
如何在一个查询中选择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,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,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_id='1' "); while($row = mysql_fetch_array($sql_all)) { echo $row['categories_id'].'<br/>'; } 谢谢. 解决方法
试试这个,
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表中的所有记录. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |