[英]SQL Select all columns from one table and Max value of another column on another table
This is my first question in stackoverflow and I hope I get an answer soon to my problem. 这是我在stackoverflow中的第一个问题,我希望我能尽快得到答案。 :) I tried searching for quite some time from other sources but unfortunately, couldn't find a working answer. :)我试图从其他来源搜索相当一段时间,但不幸的是,找不到一个有效的答案。
So, I am working on a project and since I am a newbie to sql, I cannot do this: 所以,我正在研究一个项目,因为我是sql的新手,我不能这样做:
I have 2 tables: 我有2张桌子:
"Customers" with columns "id", "name", "last name" ("id" is primary key) 列“id”,“name”,“last name”(“id”是主键)的“Customers”
"Sessions" with columns "id", "Customer", "entrydate" ("id" is primary key) 列“id”,“Customer”,“entrydate”(“id”是主键)的“Sessions”
"Customer" from "Sessions" is tied to "id" from "Customers". “会话”中的“客户”与“客户”中的“ID”相关联。 (foreign key) (外键)
I need a query that returns all columns from table "Customers" with one additional column showing the entrydate of the latest "Sessions" record, of each Customer of course. 我需要一个查询,返回表“Customers”中的所有列,其中一个附加列显示每个客户的最新“会话”记录的入口日期。 "Sessions" table may have many records for an individual "Customers" record, as you can imagine. 正如您可以想象的那样,“会话”表可能包含许多个人“客户”记录的记录。
Thanks everybody in advance and hope to get an answer soon. 提前感谢大家,希望尽快得到答案。
I may be missing something really obvious but this sounds really really basic sql the kind you'd find in a sql tutorial https://www.w3schools.com/SQL/sql_groupby.asp 我可能会遗漏一些非常明显的东西,但这听起来真的非常基本的sql你在sql教程中找到的那种https://www.w3schools.com/SQL/sql_groupby.asp
SELECT C.name,c.lastName,MAX(S.entryDate) FROM customers C
inner join Sessions S ON S.CustomerId=C.Id
group by C.name,C.lastName
就这么简单。
SELECT C.id,C.name,c.lastName,MAX(S.entryDate) as lastEntry FROM customers C join Sessions S ON S.CustomerId=C.Id group by C.id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.