如何从两个表中创建一个查询,其中每个孩子都被分配给其父母。
1个表table_one
+---+------------+-------------+---------+
|id | prj_id | name | status |
+---+------------+-------------+---------+
| 1 | 808361 | projectOne | open |
| 2 | 808365 | projectTwo | close |
| 3 | 808370 | projectThree| open |
+---+------------+-------------+---------+
2表table_two
+---+------------+---------+
|id | prj_id | task |
+---+------------+---------+
| 1 | 808361 | java |
| 2 | 808365 | c++ |
| 3 | 808365 | python |
| 4 | 808370 | java |
| 5 | 808370 | c++ |
| 6 | 808370 | python |
+---+------------+---------+
我的MySQL查询:
SELECT tt.prj_id, tt.taks, to.status
FROM table_two tt
INNER JOIN table_one to ON to.prj_id = tt.prj_id
MySQL查询的结果
+---+------------+---------+--------+
|id | prj_id | task | status |
+---+------------+---------+--------+
| 1 | 808361 | java | open |
| 2 | 808365 | c++ | close |
| 3 | 808365 | python | close |
| 4 | 808370 | java | open |
| 5 | 808370 | c++ | open |
| 6 | 808370 | python | open |
+---+------------+---------+--------+
现在我想得出以下结果,但我不知道如何继续
我想要的输出:
+---+------------+---------+--------+
|id | prj_id | task | status |
+---+------------+---------+--------+
| 1 | 808361 | - | - |
| 2 | 808361 | java | open |
| 3 | 808365 | - | - |
| 4 | 808365 | c++ | close |
| 5 | 808365 | python | close |
| 6 | 808370 | - | - |
| 7 | 808370 | java | open |
| 8 | 808370 | c++ | open |
| 9 | 808370 | python | open |
+---+------------+---------+--------+
或者我可以用Python或其他方式解决问题。
非常感谢您的帮助
答案 0 :(得分:-1)
如果您执行以下操作
select tableone.id, tableone.prj_id, tabletwo.task, tableone.status, tableone.id
from tableone, tabletwo
where tabletwo.prj_id = tableone.prj_id;
结果将是
id, prj_id, task, status, id(parent)
'1', '808361', 'java', 'open', '1'
'2', '808365', 'c++', 'closed', '2'
'2', '808365', 'python', 'closed', '2'
'3', '808370', 'java', 'open', '3'
'3', '808370', 'c++', 'open', '3'
'3', '808370', 'python', 'open', '3'
现在要创建父ID,您还可以使用一些python逻辑原因,有效的方法将是使您要通过python查找的父ID。
python逻辑将如下所示 对于第一个项目ID(如果已打开),则父ID将为另一个,然后为0,然后递增1,并检查项目2,反之亦然
希望你得到这个