I have two tables names book_list
and book_category
. Schema is like below:
book_list
+---------+--------+-----------+
| id_book | id_cat | book_name |
+---------+--------+-----------+
| 1 | 3 | Book Nam1 |
| 2 | 1 | Book Nam2 |
| 3 | 2 | Book Nam3 |
+---------+--------+-----------+
book_category
+--------+----------+-----------+-------+
| id_cat | cat_name | id_parent | level |
+--------+----------+-----------+-------+
| 1 | name1 | 0 | 1 |
| 2 | name1.1 | 1 | 2 |
| 3 |name1.1.1 | 2 | 3 |
+--------+----------+-----------+-------+
code tried:
SELECT a.id_book, a.id_cat, a.book_name, b.cat_name, b.level
FROM book_list a
INNER JOIN book_category b ON a.id_cat = b.id_cat
I need to manipulate the result array to add breadcrumb where each has link to itselfs like:
table row:
<td>3(id_book)</td>
<td>Book Nam3 (book_name)</td>
<td>
<a href="handle.php?id=<?=id_cat;?>">Name 1</a> »
<a href="handle.php?id=<?=id_cat;?>">Name 1.1</a> »
<a href="handle.php?id=<?=id_cat;?>">Name 1.1.1</a>
</td>
Hope I made my point and thank you for any help.
Try this query.
SELECT
bl.id_book, bl.id_cat, bl.book_name, tc2.cat_name, tc2.level
FROM
book_list bl
JOIN book_category tc
ON tc.id_cat = bl.id_cat
JOIN book_category tc2
ON LOCATE(tc2.cat_name, tc.cat_name) > 0
WHERE
tc.id_cat = 3
This query is based on occurrence of cat_name
substrings.
TRY
SELECT a.id_book, a.id_cat, a.book_name , b.cat_name, b.level
FROM book_list a
JOIN book_category b ON USING(id_cat)
and then
<a href="handle.php?id=<?=$id_cat;?>"><?=$cat_name?></a>