简体   繁体   English

SQL选择一个表中的所有列和另一个表上另一列的最大值

[英]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.

相关问题 SQL Server从一列中选择最大值,然后检查该值是否存在于另一张表中 - SQL Server select the max value from one column and check if the value exists in another table SQL:如何从一个表中选择最大值,而从另一表中选择其他数据 - SQL: how to select max value from one table and other data from another table Select 一个表的所有列和另一个表的一些列 - Select all columns from one table and some from another table SQL-从表中选择列,其中另一个表的列值等于列名列表 - SQL - Select Columns from table where another table column value equals column name list SQL从一个表列中选择一个最小值,然后将结果插入一个SQL语句的另一个表列中 - Sql Select a minimum value from a table column and insert the results in another table column in one SQL statement SQL从一个表中选择两列,由另一表中的列翻译 - SQL Select two columns from one table translated by a column from another table sql:另一个表中 2 列的最大值 - sql: max value by 2 columns in another table 如何从一个表中选择两列连接到SQL中另一表中的列? - How to select two columns from one table that are connected to a column in another table in SQL? 从联接的表中,选择一列的最大值,但是如果有多个maxes,则从另一列中选择具有最大值的那个。 - from a joined table, select the max value of a column, but if there are multiples maxes, pick the one that has the max value from another column 从一个表中选择所有列,从另一个表中选择一个列,其中列等于变量 - Select all columns from one table and one from another where column equals variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM