【问题标题】:How do I compare values from different column and different row but same table?如何比较来自不同列和不同行但同一张表的值?
【发布时间】:2015-05-10 22:01:11
【问题描述】:
ID Name FatherID Birthyear

1   Bart    NULL     1756    
2   Franz   1        1796
3   Josef   2        1835    
4   Zohan   3        1887

假设我有这张表,我想知道 Zohan 是否是 Bart 的儿子,如果我将“FatherID”列中的值与之前行的 ID 进行比较,直到我到达 Bart .但是如何比较同一个表中不同行和列的值

【问题讨论】:

标签: mysql sql plsql plsqldeveloper


【解决方案1】:

你可以自己加入表格:

SELECT s.name AS son_name, f.name AS father_name
FROM   mytable s
JOIN   mytable f ON s.fatherID = f.id
-- possibly add a where clause with conditions on son/father names

【讨论】:

  • 我相信我需要在 PL/SQL 中使用循环来解决这个问题,但您仍然回答了这个问题。谢谢!
猜你喜欢
  • 2021-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多