我可以对一行结果进行层次结构(使用来自previous question的查询),但是我不知道当它有多行时从哪里开始。
这是我的桌子:
public class NumGeneratorThreadSafe{
List<Integer> numberPool = new ArrayList<>();
private int counter = 0;
public NumGeneratorThreadSafe() {
for(int i = 1; i <= 1000 ; i++) {
this.numberPool.add(i);
}
Collections.shuffle(this.numberPool);
}
public synchronized Integer getRandomNumber() {
return this.numberPool.get(this.counter++);
}
public synchronized void resetCounter() {
this.counter = 0;
}
}
具有空
public class MultiThreadingRandom {
public static void main(String[] args) throws InterruptedException {
ExecutorService executorService = Executors.newFixedThreadPool(2);
NumGeneratorThreadSafe numGenerator = new NumGeneratorThreadSafe();
GeneRan r1 = new GeneRan(numGenerator);
GeneRan r2 = new GeneRan(numGenerator);
executorService.submit(r1);
executorService.submit(r2);
executorService.shutdown();
}
}
class GeneRan implements Runnable{
private NumGeneratorThreadSafe numGenerator;
public GeneRan(NumGeneratorThreadSafe numGenerator) {
this.numGenerator = numGenerator;
}
@Override
public void run() {
int randInt = this.numGenerator.getRandomNumber();
System.out.println(randInt);
}
}
的 id| parent_id| name|
-------|-----------|--------------------------|
1| [NULL]| United States|
2| [NULL]| Japan|
3| 1| New York|
4| 3| Brooklyn|
5| 6| Los Angeles|
6| 1| California|
7| 6| Dixon|
8| 2| Kyoto|
9| 2| Tokyo|
是祖父母。那么,如何进行查询以生成这样的结果?
name
答案 0 :(得分:2)
看起来像桌子上的一个简单的自我联接,因为您感兴趣的级别数量有限:
select co.name as country,
p.name as "State/Province",
ct.name as city
from hierarchy co
left join hierarchy p on p.parent_id = co.id
left join hierarchy ct on ct.parent_id = p.id
where co.parent_id is null
order by co.name;