Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to create subqueries in SQFLite ? #1083

Open
danielrrys opened this issue Feb 13, 2024 · 0 comments
Open

Is it possible to create subqueries in SQFLite ? #1083

danielrrys opened this issue Feb 13, 2024 · 0 comments

Comments

@danielrrys
Copy link

danielrrys commented Feb 13, 2024

My problem is the following, I want to make a query where I bring all (*) of a table (ELEMENT) and another table (IMAGE) may have 3 different images, but I only want to bring the first element, unfortunately with my query brings me 3 times the same ELEMENT but with different IMAGE since I have 3.

I want to bring 1 image from table A, B and C, but in the table B or C i could have 4 or 5 images extra.

I tried with DISTINCT but it doesn't work.

I also don't understand why SQFLite doesn't support subqueries, if I want to do another SELECT, why doesn't it allow me to do it?

 final List<Map<String, Object?>> res = await database.rawQuery(
        '''
        SELECT DISTINCT
          e.*, 
            a.id AS aId, a.name AS aName,
            [...]
               b.id AS bId, b.name AS bName,
               [...]
                  c.id AS cId, c.name AS cName,
                  [...]
        FROM element_table e
          LEFT JOIN image_table a ON e.imageId = a.id
          LEFT JOIN element_picture_table ep ON ep.elementId = e.id /// nested table with ids between PICTURE TABLE and ELEMENT TABLE
          LEFT JOIN picture_table p ON ep.pictureId = p.id
          LEFT JOIN image_table b ON b.id= p.imageOne /// Here in the IMAGE TABLE B, i could have 2 or 5 images... 
          LEFT JOIN image_table c ON c.id= p.imageTwo 
        WHERE [...]
        ORDER BY [...]
      ''',
        [
          ...
        ],
      );

I try to do this because i don't want to use dart logic, this to avoid a lot of process extra, imagine that I have 2000 rows in the database? if I do some kind of map or for, my process takes time.

This can be mi Object :

class Element {
  final int id,
  final String name,
  final int imageId,
  final List<Picture> pictures,
}

class Picture {
  final int id,
  final String name,
  final int imageOne,
  final int imageTwo
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant