SQFLite : Problem with CASE Statement - sub-select returns 12 columns - expected 1

27 views Asked by At

I know SQFLite is not the same as SQLite, but there must be a way for what I want.

I would like to add to my select 12 columns depending on a CASE, if it comes NULL it will add the same 12 columns but with other conditions, other tables and JOINS.

But the return of the case expects 1, and I return 12, is there any way to add those 12 in my select without having to do a logic out of my query ?


      final List<Map<String, Object?>> res = await database.rawQuery(
        '''
        SELECT 
          h.*,
          CASE
            WHEN h.animalId IS NOT NULL THEN (
              SELECT a.id as animalId, [...]
              FROM animalTable a 
              WHERE (h.animalId = a.id)
              )
            ELSE (
               SELECT a.id as animalId, [...]
              FROM animalTable a
                JOIN raceTable r ON r.id = a.raceId
                [...joins...]
              WHERE (...)
            )
          END
        FROM humanTable h
        WHERE [...]
        LIMIT 5
      ''',
        [
          ...
        ],
      );

I was thinking of using a UNION but I still have an error because in my SELECT it brings 1, and it said that it did not match the values or rows.

The ouput that i want in the query could be :

[{humanId: 1, humanName: "Karen", animalId: 1, animalName: "Doggy", ...}]

But the result doesn't work because the CASE statement expect only 1

0

There are 0 answers