如何比较另一个查询结果中的两列?

huangapple go评论58阅读模式
英文:

How to compare two columns from results of another query?

问题

当我在destination列中搜索“Queenstown”时,我想获得以下结果:

id sequence destination
3 2 东伦敦
3 3 格伯哈
英文:

Table:

id sequence destination
2 1 Johannesburg
2 2 Durban
2 3 Cape Town
3 1 Queenstown
3 2 East London
3 3 Gqeberha

When I search for "Queenstown" in the destination column I would like to get:

id sequence destination
3 2 East London
3 3 Gqeberha

I want records where sequence is greater than that of the queried record of which id is the same.

答案1

得分: 1

使用自连接:

SELECT t1.*
FROM yourTable AS t1
JOIN yourTable AS t2 ON t1.id = t2.id AND t1.sequence > t2.sequence
WHERE t2.destination = 'Queenstown'
ORDER BY t1.sequence

t1.id = t2.id 使 id 相同,t1.sequence > t2.sequence 使序列高于当前记录。

英文:

Use a self-join:

SELECT t1.*
FROM yourTable AS t1
JOIN yourTable AS t2 ON t1.id = t2.id AND t1.sequence > t2.sequence
WHERE t2.destination = 'Queenstown'
ORDER BY t1.sequence

t1.id = t2.id makes the id the same, and t1.sequence > t2.sequence makes the sequence higher than the current record.

huangapple
  • 本文由 发表于 2023年6月18日 18:12:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76500015.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定