我存储多级别类别。表格如下:
+-------------+---------------+--------------------+
| category_id | category_name | parent_category_id |
+-------------+---------------+--------------------+
| 1 | Clothing | NULL |
| 2 | Footwear | NULL |
| 3 | Men | 1 |
| 4 | Women | 1 |
| 5 | Shirts | 3 |
| 6 | T-Shirts | 3 |
| 7 | Jeans | 3 |
+-------------+---------------+--------------------+
+------------+------------+-------------+
| cmp_cat_id | company_id | category_id |
+------------+------------+-------------+
| 1 | 7 | 5 |
| 2 | 7 | 6 |
| 3 | 7 | 7 |
+------------+------------+-------------+
我想根据company_id从类别表中获取所有类别,子类别,子子类别。我很困惑这是怎么回事。我可以为商店公司类别创建一个新表吗?
我想在下拉列表中显示输出。 我有三个下拉列表,一个是主要类别下拉列表,第二个是子类别下拉列表,第三个是子类别下拉列表。下拉是明智的,即不同的公司有不同的类别。
输出显示屏幕。
抱歉我的英文。
感谢您帮助我。
答案 0 :(得分:1)
试试这段代码。
SELECT ROW_NUMBER()OVER(ORDER BY StartDate,BatchName) BatchId,
BatchName, StartDate, NoOfTrainee,
CEILING(SUM(NoOfTrainee) OVER(ORDER BY StartDate,BatchName) / 100.0) as LotNo
FROM BatchTable
关于company_id
SELECT c1.`category_name` as `category` , c2.`category_name` as `sub-category`, c3.`category_name` as `sub-sub-category` FROM `categories` as c1
LEFT JOIN `categories` as c2 ON c1.`category_id`=c2.`parent_category_id`
LEFT JOIN `categories` as c3 ON c2.`category_id`=c3.`parent_category_id`
where c1.`parent_category_id` IS NULL
如果您需要获取特定company_id的数据。
例如:company_id = 7
SELECT cm.`company_id`, c1.`category_name` as `category` , c2.`category_name` as `sub-category`, c3.`category_name` as `sub-sub-category` FROM `categories` as c1
LEFT JOIN `categories` as c2 ON c1.`category_id`=c2.`parent_category_id`
LEFT JOIN `categories` as c3 ON c2.`category_id`=c3.`parent_category_id`
INNER JOIN `company_category_mapping` as cm ON cm.`category_id`=c3.`category_id`
where c1.`parent_category_id` IS NULL
答案 1 :(得分:0)
试试此代码
我正在使用mysqli来避免问题和黑客入侵
$results = $mysqli->query( "SQL
SELECT *
FROM `categories` as c1
LEFT JOIN `categories` as c2 ON c1.`category_id`=c2.`parent_category_name`
LEFT JOIN `categories` as c3 ON c2.`category_id`=c3.`parent_category_name`
where c1.`parent_category_name` IS NULL");echo 'Results is: ' . $result;