dongye9071 2014-06-24 19:52 采纳率: 0%
浏览 45
已采纳

从3个表中选择分组表

I have 3 tables in MySQL.

excel - is the table where I store site-name, and side-id analys_result - is the table where I store keywords of sites, it has keyword, id of the category, id of site. categories - contain name and id of category

Here are screenshot's of tables, and what I need to retrieve:

Excel table enter image description here

analys_result table
enter image description hereCategory

Result enter image description here

  • 写回答

1条回答 默认 最新

  • dongqiongzheng0615 2014-06-24 20:58
    关注

    Using just sql, the best you can get is:

    select e.id, e.url, c.CategoryName, a.Keywords, count=sum(a.count)
    from excel e, categories c, analys_results a
    where e.id = a.websiteid
    and a.categoryid = c.id
    group by e.id, e.url, c.CategoryName, a.keywords
    order by e.id, e.url, c.CategoryName, a.keywords
    

    This will get you the correct data, but the grouping fields (id, url, categoryname) will show on every row in the result set (not ONCE per group), and making the query work that way using complicated SQL is NOT recommended. Let the database give you the data, and let the UI or Reporting Tool you are using determine how you want to display the results.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?