-1

So guys updating my problem: I have a Delphi 2007 function that "ImportStudentMisses":

procedure TWeb.ImportStudentMisses;
var
  SLMisses, SLClasses: TStringList;
  i: Integer;
  idClass, idStudent: integer;
begin
  SLMisses := TStringlist.Create;
  SLMisses.Duplicates := dupIgnore;
  SLMisses.Sorted := true;

  SLClasses := TStringlist.Create;
  SLClasses.Duplicates := dupIgnore;
  SLClasses.Sorted := true;

  try
    FTexto := '[INTERNET] - Fase 1';
    Synchronize(DoStatus);

    qryDestino.SQL.Clear;
    qryDestino.SQL.Add('SELECT ClassID, StudentID ');
    qryDestino.SQL.Add(' FROM ' + Config.InternetConfig.prefix + 'MissOneClass ');
    qryDestino.SQL.Add(' ORDER BY ClassID, StudentID');
    qryDestino.Open;
    while not qryDestino.eof do
    begin
      SLAulas.Add(formatfloat('00000000', qryDestino.Fields[0].AsInteger));
      SLFaltas.Add(formatfloat('00000000', qryDestino.Fields[0].AsInteger) + '-' + qryDestino.Fields[1].asString);
      qryDestino.Next;
    end;

    FTexto := '[INTERNET] - Fase 2';
    Synchronize(DoStatus);

    qryDestino.Close;
    qryDestino.SQL.Clear;
    qryDestino.SQL.Add('SELECT Date, DisciplineID, StudentID ');
    qryDestino.SQL.Add(' FROM ' + Config.InternetConfig.prefix + 'MissTwoClasses ');
    qryDestino.SQL.Add(' ORDER BY Date, DisciplineID, StudentID');
    qryDestino.Open;
    while not qryDestino.eof do
    begin //local Table
      qryOrigem.SQL.Clear;
      qryOrigem.SQL.Add('SELECT lNum FROM Class');
      qryOrigem.SQL.Add('WHERE lDiscipline = ' + IntTostr(qryDestino.Fields[1].AsInteger));
      qryOrigem.SQL.Add('  AND lDate = ' + IntTostr(trunc(qryDestino.Fields[0].AsDateTime)));
      qryOrigem.Open;
      qryOrigem.First;
      while not qryOrigem.eof do
      begin
        idClass := qryOrigem.Fields[0].AsInteger;
        if idClass > 0 then
        begin
          SLClasses.Add(FormatFloat('00000000', idClass));
          SLMisses.Add(formatfloat('00000000', idClass) + '-' + qryDestino.Fields[2].asString);
        end;
        qryOrigem.Next;
      end;
      qryOrigem.Close;

      qryDestino.Next;
    end;

    FTexto := '[INTERNET] - Fase 3';
    Synchronize(DoStatus);

    qryDestino.Close;
    qryDestino.SQL.Clear;
    qryDestino.SQL.Add('SELECT Date, StudentID ');
    qryDestino.SQL.Add(' FROM ' + Config.InternetConfig.prefix + 'MissTheDay ');
    qryDestino.SQL.Add(' ORDER BY Date, StudentID');
    qryDestino.Open;

    while not qryDestino.eof do
    begin //Local Table
      qryOrigem.SQL.Clear;
      qryOrigem.SQL.Add('SELECT lNum FROM Class');
      qryOrigem.SQL.Add('WHERE lDate = ' + IntTostr(trunc(qryDestino.Fields[0].AsDateTime)));
      qryOrigem.Open;
      qryOrigem.First;
      while not qryOrigem.eof do
      begin
        idClass := qryOrigem.Fields[0].AsInteger;
        if idClass > 0 then
        begin
          SLClasses.Add(FormatFloat('00000000', idClass));
          SLMisses.Add(formatfloat('00000000', idClass) + '-' + qryDestino.Fields[1].asString);
        end;
        qryOrigem.Next;
      end;
      qryOrigem.Close;
      qryDestino.Next;
    end;
    qryDestino.Close;
    qryDestino.SQL.Clear;

    qryOrigem.Close;


    qryOrigem.SQL.Clear;
    qryOrigem.SQL.Add('DELETE FROM Misses WHERE lClass = :pClass');
    for i := 0 to SLClasses.Count - 1 do
    begin
      idClass := StrToIntDef(SLClasses[i], 0);
      qryOrigem.Params[0].AsInteger := idClass;
      qryOrigem.ExecSQL;
    end;

    qryOrigem.SQL.Clear;
    qryOrigem.SQL.Add('INSERT INTO Misses');
    qryOrigem.SQL.Add(' (lClass, lStudent) VALUES ');
    qryOrigem.SQL.Add(' (:pClass, :pStudent)');

    for i := 0 to SLMisses.Count - 1 do
    begin
      idClass := StrToIntDef(Copy(SLMisses[i], 1, 8), 0);
      idStudent := StrToIntDef(Trim(Copy(SLMisses[i], 10, 8)), 0);

      if (idClass > 0) and (idStudent > 0) then
      begin
        try
          qryOrigem.Params[0].AsInteger := idClass;
          qryOrigem.Params[1].AsInteger := idStudent;
          qryOrigem.ExecSQL;
        except
          on e: Exception do
          begin
            FTexto := '[INTERNET] - Error ' + E.Message;
            Synchronize(DoStatus);
          end;
        end;
      end;
    end;
  except
    on E: Exception do
    begin
      FTexto := '[INTERNET] - ' + E.Message;
      Synchronize(DoStatus);
      reconect;
    end;
  end;
  SLMisses.Free;
end;

This function import all the students that miss a class in a respective day. And I have to make this function become a JSON like this:

[{"ClassID":10,"StudentsID":[1,2]},{"ClassID":20,"StudentsID":[3,4]}]

As you can see the JSON has the ("ClassID":10) and a list of students that are missing that class ("Students":[1,2,3,4,5]). I've already tryied the code below to make the JSON that I wanted.

$query = $database->query( "SELECT ClassID, GROUP_CONCAT(StudentID) AS StudentID FROM {$pfx}MissOneClass
GROUP BY ClassID ORDER BY ClassID");
$list = array();
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
    $list[] = array('ClassID' => $row['ClassID'], 
    'StudentID' => array_map('intval', explode(',', $row['StudentID'])));
    }
echo json_encode($list);

And I've got this JSON back:

[{"ClassID":1,"StudentsID":[1,2,3,4]},{"ClassID":2,"StudentsID":[3,4,23]}]

The JSON is the JSON that I needed. But I'be been told that I have to use all the tables not only "MissOneClass" as I did.

This JSON will be consumed by a JAVA application that will INSERT the JSON data on the DB. And that's the role problem.

Thank you!!!

  • Why you can't with the others queries ? – executable Jan 14 '19 at 13:16
  • do you want the union of the 3 queries ? in your JSON? or you have the option to use join? – Sayed Mohd Ali Jan 14 '19 at 13:16
  • I need that those three queries become a single JSON @SayedMohdAli it doesn't matter how. So yes I have the option to use join, but I'm not good with mysql yet – Peter Coast Jan 14 '19 at 13:19
  • 1
    @PeterCoast then you can merge the result of 3 queries using union in SQL then you can json_encode your result. – Sayed Mohd Ali Jan 14 '19 at 13:21
  • alternative you can run the 3 queries and make json of all 3 then concate the json object using => jsonArray1.concat(jsonArray2); – Sayed Mohd Ali Jan 14 '19 at 13:24
  • These are queries returning differently structured results, so using UNION probably makes rather little sense. You most likely want something like `json_encode(['query1' => $sql1, 'query2' => $sql2, 'query3' => $sql3]);` – misorude Jan 14 '19 at 13:25
  • @SayedMohdAli can you give me an example of how I could use union in SQL with those three queries? because I'm not good at SQL. – Peter Coast Jan 14 '19 at 13:28
  • @misorude What if I want to generate a JSON like this now: [{"ClassID":10,"StudentsID":[1,2]},{"ClassID":20,"StudentsID":[3,4]}] still with those three queries? – Peter Coast Jan 14 '19 at 13:40
  • @SayedMohdAli What if I want to generate a JSON like this now: [{"ClassID":10,"StudentsID":[1,2]},{"ClassID":20,"StudentsID":[3,4]}] still with those three queries? – Peter Coast Jan 14 '19 at 13:42
  • _“What if I want to generate a JSON like this now: […] still with those three queries?”_ - I don’t see how that makes sense, only your first query returns a class id and student id, your other queries return different fields in the first place. – misorude Jan 14 '19 at 13:49
  • you can't make array of studentsID but it can be make like otherway in json format : {"ClassID":20,"StudentsID1":3,"StudentsID2" :4, "studentID3":6} you have 3 student ids if you want to separate student id of each table – Sayed Mohd Ali Jan 14 '19 at 13:59
  • @PeterCoast Rather than continue this comments discussion, please update your question with examples of the output you want to achieve. – Aleks G Jan 14 '19 at 14:16
  • @AleksG You're right man! Sorry if there is a lot of code guys but that's my full problem! – Peter Coast Jan 14 '19 at 15:04

1 Answers1

0

In Union, the number of columns should be the same in all 3 tables you are using. check the different between union and union all. run the query on your database to check the result.

SELECT ClassID, StudentID, null as Date, null as DisciplineID
FROM MissOneClass
union all
SELECT null as ClassID,StudentID, Date, DisciplineID
FROM MissTwoClasses
union all
SELECT null as ClassID, StudentID, Date, null as DisciplineID
FROM MissTheDay
ORDER BY Date, DisciplineID, ClassID, StudentID

You can also use join, it is still confusing in what way you want to merge your JSON these 2 will give different results. I am assuming student id is a foreign key in this case.

SELECT ClassID, StudentID, Date, DisciplineID
FROM MissOneClass mo full outer join MissTwoClasses mt on mt.StudentID = mo.StudentID full outer join MissTheDay md md.StudentID = mo.StudentID
Sayed Mohd Ali
  • 2,156
  • 3
  • 12
  • 28