选择最受欢迎的商品

时间:2016-05-05 14:51:54

标签: mysql select

我有以下查询:

<div id="ScheduleWrapper" class="schedule-wrapper">
  <table id="ScheduleTable" class="schedule">
    <thead>
      <tr class="header-row">
        <!--header row 1-->
        <th scope="col" class="header-col">classroom</th>
        <!--col 1-->
        <th scope="col">A</th>
        <!--col 2-->
        <th scope="col">C</th>
        <!--col 3-->
        <th scope="col">D</th>
        <!--col 4-->
        <th scope="col">E</th>
        <!--col 5-->
        <th scope="col">F</th>
        <!--col 6-->
        <th scope="col">I</th>
        <!--col 7-->
      </tr>
      <tr class="header-row">
        <!--header row 2-->
        <th scope="col" class="header-col">capacity</th>
        <!--col 1-->
        <th scope="col">30</th>
        <!--col 2-->
        <th scope="col">50</th>
        <!--col 3-->
        <th scope="col">100</th>
        <!--col 4-->
        <th scope="col">30</th>
        <!--col 5-->
        <th scope="col">50</th>
        <!--col 6-->
        <th scope="col">100</th>
        <!--col 7-->
      </tr>
    </thead>
    <tbody>
      <tr>
        <!--row 5-->
        <th scope="row" class="header-col">9:30 - 10:30</th>
        <!--header col 1, period 3-->
        <td>
          <p class="class-title">
            Searching on Ancestry.com 2016 (Basic Search Ideas)
          </p>
          <div class="class-descrip">
            <p>
              Searching on Ancestry has changed drastically in 2016. Learn advanced syntax and searching strategies.
            </p>
            <p>This is some blah blah tet. This is some blah blah tet. This is some blah blah tet. This is some blah blah tet. This is some blah blah tet. This is some blah blah tet. This is some blah blah tet. This is some blah blah tet.
            </p>
          </div>
          <p class="instructor">
            Mindy McLane
          </p>
          <div class="instructor-bio">
            <p>
              Mindy McLane has taught courses at each of our conferences. She is particularly adept at all things Ancestry.
            </p>
          </div>
        </td>
        <!--col 2-->
        <td>
          <p class="class-title">
            Land and Title Records
          </p>
          <div class="class-descrip">
            <p>
            </p>
          </div>
          <p class="instructor">
            Nancy Feroe
          </p>
          <div class="instructor-bio">
            <p>
            </p>
          </div>
        </td>
        <!--col 3-->
        <td>
          <p class="class-title">
            Open Computer Lab
          </p>
          <div class="class-descrip">
            <p>
            </p>
          </div>
          <p class="instructor">
            varying instructors
          </p>
          <div class="instructor-bio">
            <p>
            </p>
          </div>
        </td>
        <!--col 4-->
        <td>
          <p class="class-title">
            "Hey Dad! What did you do in the War?"
          </p>
          <div class="class-descrip">
            <p>
            </p>
          </div>
          <p class="instructor">
            Jim Johnson
          </p>
          <div class="instructor-bio">
            <p>
            </p>
          </div>
        </td>
        <!--col 5-->
        <td>
          <p class="class-title">
            Thinking through your DNA results and figuring out what to do next.
          </p>
          <p class="instructor">
            Dianne Gianninni Dianne Gianninni
            <div class="instructor-bio">
              <p>
              </p>
            </div>
        </td>
        <!--col 6-->
        <td>
          <p class="class-title" ">
                  Come Find Out What’s Available at the Family History Center in Springdale
                  </p>
                  <div class="class-descrip "><p>
                  </p>
                  </div>
                  <p class="instructor ">
                  Charlie Fowler
                  </p>
                  </div>
                </td>									<!--col 7-->
    		</tr>
    		
    	</tbody>
    </table>
    </div>

如您所见,我有3个表:链接,类别和点击。点击&#39;点击&#39;每次点击一个数字时我都保存了一条记录。我想选择10个最受欢迎的链接,但我只获得所有点击合并的最后一条记录。

我的查询有什么问题?

提前致谢!

1 个答案:

答案 0 :(得分:0)

似乎你需要分组

SELECT
    l.id,
    l.name,
    l.url,
    c.name,
    COUNT(*) AS popularity
FROM
    links AS l, 
    categories AS c,
    clicks AS cc
WHERE  l.category_id = c.id
AND    l.id = cc.link_id
AND    l.active = 1
GROUP BY l.id, c.name
ORDER BY popularity DESC LIMIT 10