使用多个表格和字段

时间:2015-05-27 09:07:45

标签: drop-down-menu count record

我需要查询帮助,无法让它正常工作。我想要实现的是有一个选择框显示与特定主题相关的记录数量,对于某些主题它适用于某些显示(0)当实际上有2个记录时,我想知道是否有人可以帮助我,你的帮助将不胜感激,请参阅下面我的实际查询+表格结构:

SELECT theme.id_theme, theme.theme, calender.start_date,
      calender.id_theme1,calender.id_theme2, calender.id_theme3, COUNT(*) AS total 
FROM theme, calender 
WHERE (YEAR(calender.start_date) = YEAR(CURDATE()) 
    AND MONTH(calender.start_date) > MONTH(CURDATE()) ) 
    AND (theme.id_theme=calender.id_theme1) 
    OR (theme.id_theme=calender.id_theme2) 
    OR (theme.id_theme=calender.id_theme3) 
GROUP BY theme.id_theme 
ORDER BY theme.theme ASC

主题表

|---------------------|
| id_theme | theme    |
|----------|----------|
|     1    |   Yoga   |
|     2    |   Music  |
|     3    |  Taichi  |
|     4    |  Dance   |
|     5    | Coaching |
|---------------------|

CALENDAR表

|---------------------------------------------------------------------------|
| id_calender | id_theme1 | id_theme2 | id_theme3 | start_date |  end_date  |
|-------------|-----------|-----------|-----------|------------|------------|
|      1      |     2     |     4     |           | 2015-07-24 | 2015-08-02 |
|      2      |     4     |     1     |     5     | 2015-08-06 | 2015-08-22 |
|      3      |     1     |     3     |     2     | 2014-10-11 | 2015-10-28 |
|---------------------------------------------------------------------------|

LISTBOX

|----------------|
|                |
|    Yoga (1)    |
|    Music (1)   |
|   Taichi (0)   |
|    Dance (2)   |
|  Coaching (1)  |
|----------------|

提前感谢你

1 个答案:

答案 0 :(得分:0)

我认为主题条件应该放在括号中

((theme.id_theme=calender.id_theme1) 
OR (theme.id_theme=calender.id_theme2) 
OR (theme.id_theme=calender.id_theme3))

希望这个帮助