SQL选择ID最大的记录
我遇到了一些 SQL 问题,但无法找到解决方案。
现在我运行的查询基本上是:
SELECT Q.ID, Q.STATUS, C.LASTNAME, C.FIRSTNAME,
C.POSTAL, C.PHONE
FROM QUEUE Q
LEFT OUTER JOIN CUSTOMER C ON Q.ID = C.APPID
WHERE C.LASTNAME LIKE 'SMITH%'
我从这个查询中得到了大约 200 条记录。我的问题是同一个人多次出现。
Q.ID Q.STATUS C.LASTNAME C.FIRSTNAME ETC...
1 A SMITH JOHN
2 A SMITH RYAN
3 B SMITH BRIAN
100 A SMITH RYAN
200 A SMITH RYAN
我需要返回的是
Q.ID Q.STATUS C.LASTNAME C.FIRSTNAME ETC...
1 A SMITH JOHN
3 B SMITH BRIAN
200 A SMITH RYAN
任何人都可以指出我正确的方向吗?我尝试过
SELECT WHATEVER FROM TABLE WHERE Q.ID IN (SELECT MAX(ID) FROM TABLE WHERE BLAH BLAH)
专门搜索“RYAN SMITH”时有效的方法。但我需要显示 ID 最高的 SMITH 的所有结果。
任何帮助表示赞赏。
干杯
I have a issue with some SQL that I can't wrap my head around a solution.
Right now the query I am running basically is:
SELECT Q.ID, Q.STATUS, C.LASTNAME, C.FIRSTNAME,
C.POSTAL, C.PHONE
FROM QUEUE Q
LEFT OUTER JOIN CUSTOMER C ON Q.ID = C.APPID
WHERE C.LASTNAME LIKE 'SMITH%'
I have about 200 records from this query. My issue is the same person has multiple occurances.
Q.ID Q.STATUS C.LASTNAME C.FIRSTNAME ETC...
1 A SMITH JOHN
2 A SMITH RYAN
3 B SMITH BRIAN
100 A SMITH RYAN
200 A SMITH RYAN
What I need returned instead is
Q.ID Q.STATUS C.LASTNAME C.FIRSTNAME ETC...
1 A SMITH JOHN
3 B SMITH BRIAN
200 A SMITH RYAN
Can anyone point me in the right direction please. I have tried
SELECT WHATEVER FROM TABLE WHERE Q.ID IN (SELECT MAX(ID) FROM TABLE WHERE BLAH BLAH)
which worked when searching for "RYAN SMITH" specifically. But I need to show all results for SMITH with the highest IDs.
Any help is appreciated.
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我想你可以做如下的事情
希望这有帮助!
I guess you could do something like below
Hope this helps!!
你尝试过类似的事情吗?好吧,这并不是一个真正的 SQL 语句,只是展示了这个想法
have you tried something like that? well it is not really a SQL statement, just showing the idea
如果我理解得很好,这应该可行:
注意:我已将“左连接”更改为“内连接”,因为您正在按 C.LASTNAME 进行过滤。所以我认为左连接没有多大意义。
If I understand well, this should work:
Note: I have change "left join" by "inner join" because you are filtering per C.LASTNAME. So I think left join doesn't have many sense.
假设有一个 CUSTOMER.ID,并且我认为我是对的,如下所示:
Assuming there is a
CUSTOMER.ID
, and I think I'm right, here it goes: