1回のクエリで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_id='1' ");
while($row = mysql_fetch_array($sql_all)) {
echo $row['categories_id'].'<br/>';
}
ありがとう。