I have a PHP function that returns me an array with category information. It is supposed to be provided with a $categoryId, and then return that category and any category "above" it. This is working:
Table structure of tbl_categories
:
CREATE TABLE `tbl_categories` (
`categoryId` int(11) NOT NULL,
`categoryParentId` int(11) NOT NULL,
`categoryName` varchar(50) NOT NULL,
`categoryDescr` varchar(400) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `tbl_categories` ADD PRIMARY KEY (`categoryId`);
ALTER TABLE `tbl_categories`
MODIFY `categoryId` int(11) NOT NULL AUTO_INCREMENT;
COMMIT;
Code of MySQL query:
SELECT * FROM (
SELECT
@r AS _id,
(SELECT @r := categoryParentId FROM tbl_categories WHERE categoryId = _id) AS categoryParentId,
@l := @l + 1 AS categoryDepth
FROM
(SELECT @r := '.$categoryId.', @l := 0) vars,
tbl_categories h
WHERE @r <> 0) T1
JOIN tbl_categories T2 ON T1._id = T2.categoryId
ORDER BY T1.categoryDepth DESC
But now, instead of one table I have two:
- tbl_categories
- tbl_categories_custom
They are identical in structure. The categoryId
in tbl_categories_custom
starts from 1000 and will not "collide" with the ones in tbl_categories
Categories in tbl_categories_custom
can have their categoryParentId
set to a categoryId
in tbl_categories
.
Now, my question is, how can I change my query above that works with just tbl_categories
, to also work with tbl_categories_custom
.
I think I must UNION these tables first somehow, but I don't understand how.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…