如何完全获取所选列和sum列

时间:2018-03-24 20:20:16

标签: sql

SELECT f_name,l_name,teachers.first_name,teachers.t_id,p_id,paid_amount,family_id,date,sum(payments.paid_amount)
FROM payments
LEFT JOIN family ON family.id = payments.family_id
LEFT JOIN teachers ON family.teacher_id = teachers.t_id

如何完全获取所选列和sum列? 因为sum函数使所有选定的结果成为一行

SELECT f_name,l_name,teachers.first_name,teachers.t_id,p_id,paid_amount,family_id,date
FROM payments
LEFT JOIN family ON family.id = payments.family_id
LEFT JOIN teachers ON family.teacher_id = teachers.t_id

此查询在没有总和列

的情况下正常工作

3 个答案:

答案 0 :(得分:0)

您没有告诉数据库,哪个列用于聚合数据。不知道您正在使用哪个数据库,但有些人抱怨说SQL文本中没有GROUP BY语句。

请尝试使用以下查询:

SELECT f_name,l_name,teachers.first_name,teachers.t_id,p_id,paid_amount,family_id,date,sum(payments.paid_amount)
FROM payments
LEFT JOIN family ON family.id = payments.family_id
LEFT JOIN teachers ON family.teacher_id = teachers.t_id
GROUP BY f_name,l_name,teachers.first_name,teachers.t_id,p_id,paid_amount,family_id,date

GROUP BY告诉数据库,这是聚合中的关键列。

答案 1 :(得分:0)

如果您想要所有付款,请使用子查询或class ViewModel { func getImages() -> [UIImage] { var images: [UIImage] = [] for i in 0..<44 { if let image = UIImage(named: "hotDog\(i).png") { images.append(image) } } return images } } class ViewController: UIViewController { private var viewModel: ViewModel! func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let cell = tableView.cellForRow(at: indexPath) as! CustomCell let images = viewModel.getImages() cell.animateImageView(withImages: images) } } class CustomCell: UITableViewCell { @IBOutlet weak var imageView: UIImageView! func animateImageView(withImages images: [UIImage]) { imageView.image = images.last imageView.animationImages = images imageView.animationDuration = TimeInterval(images.count / 20) imageView.animationRepeatCount = 1 imageView.startAnimating() } }

join

答案 2 :(得分:0)

SELECT f_name,l_name,t.first_name,t.t_id,p_id,paid_amount,family_id,date,sum(p.paid_amount)
FROM payments p,family f,teachers t where f.id = p.family_id and f.teacher_id = t.t_id
Group by f_name,l_name,teachers.first_name,teachers.t_id,p_id,paid_amount,family_id

您可以根据您的要求在Group by表达式中添加日期列。例如:

f_name,l_name,teachers.first_name,teachers.t_id,p_id,paid_amount,family_id,date