Php Mysql分类列表卡住了

时间:2014-02-16 22:40:30

标签: mysql sql

bx_ads_category(主要类别表)

ID | Name
1  | Job
9  | Property
11 | General

bx_ads_category_subs(子类别表)

ID | IDClassified | NameSub
85 | 1            | Developer
86 | 1            | Accountant
87 | 9            | For Sale
88 | 11           | For Rent

bx_ads_main(分类列表)

ID | IDClassifiedsSubs | Subject | etc.
10 | 85                | A Company need Php Developer
11 | 85                | B Company need Php Developer
12 | 86                | C Company need Accountant
13 | 88                | Lux Apartment

我需要那样的列表

SELECT * FROM bx_ads_category WHERE ID = 1

结果

10 = A Company need Php Developer
11 = B Company need Php Developer
12 = C Company need Accountant

2 个答案:

答案 0 :(得分:2)

我认为您可以使用连接查询获得结果,如下所示

SELECT * 
FROM bx_ads_category a
INNER JOIN bx_ads_main b
ON a.IDClassifiedsSubs = b.ID
INNER JOIN bx_ads_category c
ON b.IDClassified = c.ID
WHERE c.ID = 1 

答案 1 :(得分:0)

SELECT m.id, m.subject
  FROM bx_ads_main m JOIN bx_ads_category_subs s 
    ON m.IDClassifiedsSubs = s.ID JOIN bx_ads_category c
    ON s.IDClassified = c.ID
 WHERE c.ID = 1

输出:

| ID |                      SUBJECT |
|----|------------------------------|
| 10 | A Company need Php Developer |
| 11 | B Company need Php Developer |
| 12 |    C Company need Accountant |

这是 SQLFiddle 演示