doujie3888 2014-03-31 10:05
浏览 11
已采纳

获取有关客户总访问量和收入的数据

I have two tables, tbl_customers and tbl_transactions, where the fields of them are as follows:

**tbl_customer:**

c_id  |  c_name  |  age  |  Slab  |  gender  |  occupation  |  married  |  priority  |  ratings  |  contact  |  email  |  dob  |  anniversary

**tbl_transaction**


t_id  |  c_id  |  d_id  |  f_id  |  date_time  |  bill  |  member

The table tbl_transaction is a global table containing the transactions of all departments, differentiated by d_id.

Now I want to run a query that fetches c_id, total visits(means no of rows a c_id apppears in tbl_transaction), total revenue(means SUM(bill) of a particular c_id in) and the respective priority and ratings as it is in tbl_customer.

The query I was trying is :

Select c_id,
COUNT(cid) as tot_visit,
SUM(bill) as tot_revenue, priority, ratings 
From tbl_customer c
inner join tbl_transaction t on c.c_id=t.c_id
Where $r_id='r1' AND $c_id='".$cid."'
group by c_id

So basicaly I want to display the total visits and revenue of each customer in customer table, but not sure what is the right way to do it.

Any help would be appreciated.

This is the error I am getting in SQL Engine This is the error I am getting in SQL Engine

  • 写回答

2条回答 默认 最新

  • dongqiao3214 2014-03-31 10:16
    关注

    According to your error there are fields in both tables that have same name in your tables you need to give the table alias for the field, also when you are filtering on only one group then there is no need to use group by because you are using AND c.c_id='".$cid."' for a single group

    Select c.c_id,
    COUNT(c.cid) as tot_visit,
    SUM(t.bill) as tot_revenue, c.priority, c.ratings 
    From tbl_customer c
    inner join tbl_transaction t on c.c_id=t.c_id
    Where $r_id='r1' AND c.c_id='".$cid."'
    group by c.c_id
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)