dongping1922 2016-06-10 12:52
浏览 33
已采纳

从多个表返回单个记录

I have a table structure like following:

Users => Histories <= Brands

Histories table keeps following columns: userId, brandId, points

I have a query like following:

select b.id, sum(h.points),h.brandId
from brands b left join
     histories h
     on b.id = h.brandid and h.userId = 2866
group by b.id;

This query returns me brands where USER made points and didnt make any points at all..

I wanna add a filter to a brandId as well so that the query doesn't returns all results, but a single result at a time depending what brandId has been sent to the query... How can I and where can I add a statement which would return me a single record for this query?? So basically I just need to pass brandId = to something statement, leaving the query it self as it is right now ... :)

  • 写回答

1条回答 默认 最新

  • doufu1970 2016-06-10 13:22
    关注

    For example if brand id is 100:

    select b.id, sum(h.points),h.brandId
    from brands b left join
         histories h
         on b.id = h.brandid and h.userId = 2866
    where b.id=100
    group by b.id
    limit 1;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作