id id2 body
1 5 cat
2 6 bat
3 1 dgdfgdfg
4 4 rat
5 3 sdfsdfsdfs
6 2 sdfsdf
Mysql查询得到这样的结果
id id2 body
1 sdfsdfsdfs cat
2 sdfsdf bat
3 cat dgdfgdfg
4 rat rat
5 dgdfgdfg sdfsdfsdfs
6 bat sdfsdf
我需要一个mysql查询来获得该结果。对不起,问这个基本问题。在推出之前我做了很多搜索。
答案 0 :(得分:0)
SELECT t1.id, IFNULL(t2.body, t1.id2) AS id2, t1.body
FROM YourTable AS t1
LEFT JOIN YourTable AS t2 ON t2.id = t1.id2