lc0817
leoIsCoding
2015-06-27 09:18
采纳率: 100%
浏览 2.1k

mysql 数据库 多表 复合 查询

一共有3个表,
goods表,有3个字段: id (int,PK) , price (float), name (varchar(20))
buyer表,有2个字段: id(int,PK) ,name (varchar(20))
buyRecord表,有3个字段:id(int PK), buyerId(int,FK) , goodsId(int,FK)
问题:
如何用一句或多句语句查询出buyer表中id为0的用户的消费总金额?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • u010379921
    瞌睡隆尧 2015-06-27 09:48
    已采纳

    select sum(good.price) from good inner join buyRecord on (good.id = buyRecord.goodsId) where buyRecord.buyerId = 0;

    点赞 评论
  • Melon_kk
    Melon_kk 2015-06-27 09:24

    大概是这样吧
    select count(price) from goods as gs join buyRecord as br on gs.id=br.goodsid join buyer as b on br.buyerId=b.id where b.id='1'

    点赞 评论
  • lc0817
    leoIsCoding 2015-06-27 09:31

    给出一组简单的数据
    图片说明

    点赞 评论

相关推荐