dsdfd2322 2012-09-13 18:11
浏览 23

构建SQL查询 - 返回一些行,有些不行

I need to build a SQL query which is beyond my programming abilities. Okay, here is my request:

Let's say I have a table, with: id, user_id, email and amount columns. This query, should SELECT user_id only once! If I have matched user_id once, the query shall continue, but if the same user_id is matched again in another row, we should skip it.

Here comes the main problem...

Imagining that we grabbed user_id once, and skipped all same other rows with that user_id, now I need to sum all the contents from the amount column for the same user_id.

I think I complicated this a bit, I'll try illustrating my issue:

illustration of my problem

If this problem is not solvable via SQL only, then a PHP answer would work too.

I'm trying to create a list of users (no duplicate users) and add the amount they paid.

According to the image, user_id 56 paid 90.00 (12 + 45 + 33)

Can someone tell me a way how to achieve this?

  • 写回答

1条回答 默认 最新

  • drno94939847 2012-09-13 18:13
    关注

    Assuming the name of the table is users

    SELECT user_id, email, SUM(amount) FROM users GROUP BY user_id
    
    评论

报告相同问题?

悬赏问题

  • ¥15 在matlab中Application Compiler后的软件无法打开
  • ¥15 想问一下STM32创建工程模板时遇到得问题
  • ¥15 Fiddler抓包443
  • ¥20 Qt Quick Android 项目报错及显示问题
  • ¥15 而且都没有 OpenCVConfig.cmake文件我是不是需要安装opencv,如何解决?
  • ¥15 oracleBIEE analytics
  • ¥15 H.264选择性加密例程
  • ¥50 windows的SFTP服务器如何能批量同步用户信息?
  • ¥15 centos7.9升级python3.0的问题
  • ¥15 安装CentOS6时卡住