1

我有两个表名book_listbook_category. 架构如下:

书单

+---------+--------+-----------+  
| 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   |
+--------+----------+-----------+-------+

代码尝试:

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

我需要操纵结果数组以添加面包屑,其中每个都有指向自身的链接,例如:

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>

希望我表达了我的观点,并感谢您的帮助。

4

2 回答 2

0

尝试

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)

进而

<a href="handle.php?id=<?=$id_cat;?>"><?=$cat_name?></a>
于 2012-04-09T07:08:17.657 回答
0

试试这个查询。

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

此查询基于cat_name子字符串的出现。

于 2012-04-09T07:11:28.193 回答