我有兩個表格:shop_item
(父母)和shop_products
(孩子)。獲取SQL中兒童的記錄總數
在表shop_products
有列shop_item_id
,它表明,他的父母是什麼。有些父母有很多孩子。在表shop_products
中也有專欄shop_number
,這是唯一的。我的目標是找到所有有孩子的父母,例如123
(例如)。我可以通過它achive:
SELECT shop_products.shop_number, shop_products.shop_item_id, shop_item.*
FROM shop_products,
shop_item
WHERE shop_products.shop_number LIKE '123%'
AND shop_item.id = shop_products.shop_item_id;
這一個工作,但我想還可以得到這些孩子的數量(因爲作爲結果,我得到父母的),但我不知道這是否是甚至有可能。
編輯:
實施例表shop_item
:
+----+-------+-----+
| id | name | ... |
+----+-------+-----+
| 1 | test1 | ... |
| 2 | test2 | ... |
| 3 | test3 | ... |
| 4 | test4 | ... |
+----+-------+-----+
實施例表shop_products
:
+----+--------------+-------------+-----+
| id | shop_item_id | shop_number | ... |
+----+--------------+-------------+-----+
| 1 | 1 | 12345 | ... |
| 2 | 2 | 1234567 | ... |
| 3 | 1 | 14486 | ... |
| 4 | 3 | 32333 | ... |
| 5 | 1 | 12399 | ... |
| 6 | 4 | 12325 | ... |
| 7 | 2 | 25511 | ... |
| 8 | 1 | 42387 | ... |
+----+--------------+-------------+-----+
期待的結果:
+----+----------+-----+
| id | children | ... |
+----+----------+-----+
| 1 | 2 | ... |
| 2 | 1 | ... |
| 4 | 1 | ... |
+----+----------+-----+
添加一些示例表和預期結果(只是足以描述問題)。 – jarlh
@jarlh見編輯。 – debute