majid1605
جمعه 07 شهریور 1393, 17:16 عصر
select postid, parentid, type
from (
SELECT q1.*
, lpad(q1.parentid,8,case q1.type when 'Q' then 'a'
when 'C' then 'b'
when 'A' then 'c'
end)
|| lpad(q1.postid,8,case q1.type when 'Q' then 'a'
when 'C' then 'b'
when 'A' then 'c'
end) as padded_path
FROM qa_posts q1
WHERE q1.postid=1
UNION
SELECT q2.*
, lpad(q1.parentid,8,case q1.type when 'Q' then 'a'
when 'C' then 'b'
when 'A' then 'c'
end)
|| lpad(q1.postid,8,case q1.type when 'Q' then 'a'
when 'C' then 'b'
when 'A' then 'c'
end)
|| lpad(q2.postid,8,case q2.type when 'Q' then 'a'
when 'C' then 'b'
when 'A' then 'c'
end) as padded_path
FROM qa_posts q1
JOIN qa_posts q2
on q1.postid=q2.parentid
WHERE q1.postid=1
UNION
SELECT q3.*
, lpad(q1.parentid,8,case q1.type when 'Q' then 'a'
when 'C' then 'b'
when 'A' then 'c'
end)
|| lpad(q1.postid,8,case q1.type when 'Q' then 'a'
when 'C' then 'b'
when 'A' then 'c'
end)
|| lpad(q2.postid,8,case q2.type when 'Q' then 'a'
when 'C' then 'b'
when 'A' then 'c'
end)
|| lpad(q3.postid,8,case q3.type when 'Q' then 'a'
when 'C' then 'b'
when 'A' then 'c'
end) as padded_path
FROM qa_posts q1
JOIN qa_posts q2
on q1.postid=q2.parentid
JOIN qa_posts q3
on q2.postid=q3.parentid
WHERE q1.postid=1
) as x
order by padded_path;
فیلد padded_path باید شامل یک مقدار رشته ایی بشه ولی با مقدار null پر میشه چرا؟
فقط یک نکته اینکه وقتی type=Q باشد parentid=null هستش .
وقتی خط 3 تا 6 رو حذف میکنم مشکل حل میشهو یا خطوط دیگه رو حذف میکنم ولی نتیجه بدردم نمیخوره
مقدار parentid رو صفر هم میزارم به جای null با 0 پرمیشه
select postid, parentid, type ,padded_path
from (
SELECT q1.*
, lpad(q1.postid,8,case q1.type when 'Q' then 'a'
when 'C' then 'b'
when 'A' then 'c'
end) as padded_path
FROM qa_posts q1
WHERE q1.postid=1
UNION
SELECT q2.*
, lpad(q2.postid,8,case q2.type when 'Q' then 'a'
when 'C' then 'b'
when 'A' then 'c'
end) as padded_path
FROM qa_posts q1
JOIN qa_posts q2
on q1.postid=q2.parentid
WHERE q1.postid=1
UNION
SELECT q3.*
, lpad(q3.postid,8,case q3.type when 'Q' then 'a'
when 'C' then 'b'
when 'A' then 'c'
end) as padded_path
FROM qa_posts q1
JOIN qa_posts q2
on q1.postid=q2.parentid
JOIN qa_posts q3
on q2.postid=q3.parentid
WHERE q1.postid=1
) as x
order by padded_path;
خروجی میشه همچین چیزی
+--------+----------+------+------------------+
| postid | parentid | type | padded_path |
+--------+----------+------+------------------+
| 1 | 0 | Q | aaaaaaa1 |
| 8 | 1 | C | bbbbbbb8 |
| 4 | 2 | c | bbbbbbb4 |
| 5 | 2 | C | bbbbbbb5 |
| 2 | 1 | A | ccccccc2 |
| 6 | 1 | A | ccccccc6 |
+--------+----------+------+------------------+
اما خروجی مد نظر من اینه
+--------+----------+------+----------------------------------+
| postid | parentid | type | padded_path |
+--------+----------+------+----------------------------------+
| 1 | 0 | Q | aaaaaaa0aaaaaaa1 |
| 8 | 1 | C | aaaaaaa0aaaaaaa1bbbbbbb8 |
| 2 | 1 | A | aaaaaaa0aaaaaaa1ccccccc2 |
| 4 | 2 | C | aaaaaaa0aaaaaaa1ccccccc2bbbbbbb4 |
| 5 | 2 | C | aaaaaaa0aaaaaaa1ccccccc2bbbbbbb5 |
| 6 | 1 | A | aaaaaaa0aaaaaaa1ccccccc6 |
+--------+----------+------+----------------------------------+
from (
SELECT q1.*
, lpad(q1.parentid,8,case q1.type when 'Q' then 'a'
when 'C' then 'b'
when 'A' then 'c'
end)
|| lpad(q1.postid,8,case q1.type when 'Q' then 'a'
when 'C' then 'b'
when 'A' then 'c'
end) as padded_path
FROM qa_posts q1
WHERE q1.postid=1
UNION
SELECT q2.*
, lpad(q1.parentid,8,case q1.type when 'Q' then 'a'
when 'C' then 'b'
when 'A' then 'c'
end)
|| lpad(q1.postid,8,case q1.type when 'Q' then 'a'
when 'C' then 'b'
when 'A' then 'c'
end)
|| lpad(q2.postid,8,case q2.type when 'Q' then 'a'
when 'C' then 'b'
when 'A' then 'c'
end) as padded_path
FROM qa_posts q1
JOIN qa_posts q2
on q1.postid=q2.parentid
WHERE q1.postid=1
UNION
SELECT q3.*
, lpad(q1.parentid,8,case q1.type when 'Q' then 'a'
when 'C' then 'b'
when 'A' then 'c'
end)
|| lpad(q1.postid,8,case q1.type when 'Q' then 'a'
when 'C' then 'b'
when 'A' then 'c'
end)
|| lpad(q2.postid,8,case q2.type when 'Q' then 'a'
when 'C' then 'b'
when 'A' then 'c'
end)
|| lpad(q3.postid,8,case q3.type when 'Q' then 'a'
when 'C' then 'b'
when 'A' then 'c'
end) as padded_path
FROM qa_posts q1
JOIN qa_posts q2
on q1.postid=q2.parentid
JOIN qa_posts q3
on q2.postid=q3.parentid
WHERE q1.postid=1
) as x
order by padded_path;
فیلد padded_path باید شامل یک مقدار رشته ایی بشه ولی با مقدار null پر میشه چرا؟
فقط یک نکته اینکه وقتی type=Q باشد parentid=null هستش .
وقتی خط 3 تا 6 رو حذف میکنم مشکل حل میشهو یا خطوط دیگه رو حذف میکنم ولی نتیجه بدردم نمیخوره
مقدار parentid رو صفر هم میزارم به جای null با 0 پرمیشه
select postid, parentid, type ,padded_path
from (
SELECT q1.*
, lpad(q1.postid,8,case q1.type when 'Q' then 'a'
when 'C' then 'b'
when 'A' then 'c'
end) as padded_path
FROM qa_posts q1
WHERE q1.postid=1
UNION
SELECT q2.*
, lpad(q2.postid,8,case q2.type when 'Q' then 'a'
when 'C' then 'b'
when 'A' then 'c'
end) as padded_path
FROM qa_posts q1
JOIN qa_posts q2
on q1.postid=q2.parentid
WHERE q1.postid=1
UNION
SELECT q3.*
, lpad(q3.postid,8,case q3.type when 'Q' then 'a'
when 'C' then 'b'
when 'A' then 'c'
end) as padded_path
FROM qa_posts q1
JOIN qa_posts q2
on q1.postid=q2.parentid
JOIN qa_posts q3
on q2.postid=q3.parentid
WHERE q1.postid=1
) as x
order by padded_path;
خروجی میشه همچین چیزی
+--------+----------+------+------------------+
| postid | parentid | type | padded_path |
+--------+----------+------+------------------+
| 1 | 0 | Q | aaaaaaa1 |
| 8 | 1 | C | bbbbbbb8 |
| 4 | 2 | c | bbbbbbb4 |
| 5 | 2 | C | bbbbbbb5 |
| 2 | 1 | A | ccccccc2 |
| 6 | 1 | A | ccccccc6 |
+--------+----------+------+------------------+
اما خروجی مد نظر من اینه
+--------+----------+------+----------------------------------+
| postid | parentid | type | padded_path |
+--------+----------+------+----------------------------------+
| 1 | 0 | Q | aaaaaaa0aaaaaaa1 |
| 8 | 1 | C | aaaaaaa0aaaaaaa1bbbbbbb8 |
| 2 | 1 | A | aaaaaaa0aaaaaaa1ccccccc2 |
| 4 | 2 | C | aaaaaaa0aaaaaaa1ccccccc2bbbbbbb4 |
| 5 | 2 | C | aaaaaaa0aaaaaaa1ccccccc2bbbbbbb5 |
| 6 | 1 | A | aaaaaaa0aaaaaaa1ccccccc6 |
+--------+----------+------+----------------------------------+