SQLite中的多行作为列

时间:2018-03-13 18:07:57

标签: sql sqlite

我有这三张桌子:

学生表

| _id | name      |
| --- | --------- |
| 1   | Subject 1 |
| 2   | Subject 2 |
| ... | ...       |

主题表

| student | subject |
| ------- | ------- |
| 1       | 5       |
| 1       | 4       |
| 2       | 7       |
| 3       | 8       |

他们之间的关系

| student   | subject1  | subject2  |
| --------- | --------- | --------- |
| Student 1 | Subject 5 | Subject 4 |
| Student 2 | Subject 7 | NULL      |
| Student 3 | Subject 8 | NULL      |

我想要的是创建一个如下所示的查询:

SELECT st.name AS student,
       su.name AS subject1,
       su2.name AS subject2
  FROM student AS st
       JOIN
       student_subjects AS ss ON ss.student = st._id
       JOIN
       subject AS su ON ss.subject = su._id
       JOIN
       subject AS su2 ON ss.subject = su2._id
 GROUP BY st._id;

每个学生最多有1或2个科目。

尝试

| student   | subject1  | subject2  |
| --------- | --------- | --------- |
| Student 1 | Subject 5 | Subject 5 |
| Student 2 | Subject 7 | Subject 7 |
| Student 3 | Subject 8 | Subject 8 |

但结果是

Select A.* 
from A 
where A.Id not in (select ref 
                   from B 
                   where B.ref = A.Id 
                     and B.Status = 'X')

2 个答案:

答案 0 :(得分:1)

嗯。如果您知道有两个科目:

function setNewPosition(objID, dx, dy) { //This sets the new position of the     object

  var obj = getElement(objID);
  boundaryCheck(objID, dx, dy);
  var newleft = parseInt(obj.style.left) + dx;
  var newtop = parseInt(obj.style.top) + dy;
  obj.style.left = newleft.toString() + 'px';
  obj.style.top = newtop.toString() + 'px';

}

function shape(objID, canvasID, dx, dy, delay) {
  var thisShape = this;
  this.objID = objID;
  this.dx = dx;
  this.dy = dy;
 this.speedX = 0;
  this.speedY = 0;

  thisShape.draw = function() {
    drawShape(canvasID);
  }


  thisShape.move = function() {
    setNewPosition(objID, dx, dy);
    setTimeout(thisShape.move, delay);
  }
  thisShape.stop = function() {
      clearTimeout(thisShape.move);
  }
}
    function moveObj(id) { //starts process to move the shapes. 
  document.shapeObj[id].move();
}

function stopObj(id){
    document.shapeObj[id].stop();
}

答案 1 :(得分:1)

您可以测试正常工作的演示here

SELECT S1.Name AS Student,
       MIN(S2.Name) AS Subject1,
       (CASE WHEN MIN(S2.ID) != MAX(S2.ID) THEN MAX(S2.Name) END) AS Subject2
FROM Students S1
     JOIN Relations R ON R.ID_Student = S1.ID
     JOIN Subjects AS S2 ON S2.ID = R.ID_Subject
GROUP BY Student
ORDER BY Student

或者,我发现它不必要地复杂:

SELECT
  T1.StudentName AS Student,
  T1.SubjectName AS Subject1,
  (CASE WHEN T1.SubjectName != T2.SubjectName THEN T2.SubjectName END) AS Subject2
FROM 
  (SELECT S1.ID AS StudentID, S1.Name AS StudentName, MIN(S2.Name) AS SubjectName FROM Students S1 JOIN Relations R ON R.ID_Student = S1.ID JOIN Subjects AS S2 ON S2.ID = R.ID_Subject GROUP BY StudentID ORDER BY StudentID) T1
JOIN
  (SELECT S1.ID AS StudentID, S1.Name AS StudentName, MAX(S2.Name) AS SubjectName FROM Students S1 JOIN Relations R ON R.ID_Student = S1.ID JOIN Subjects AS S2 ON S2.ID = R.ID_Subject GROUP BY StudentID ORDER BY StudentID) T2
ON
  T1.StudentID = T2.StudentID