sqlite conditional select from multiple tables

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • divideby0
    New Member
    • May 2012
    • 131

    sqlite conditional select from multiple tables

    I've got 5 tables that are related; there are other fields in each table, but these are trouble makers.

    Code:
       tbl_1
    vol_key TEXT(),
    fil_id INTEGER
    type INTEGER
    
       tbl_2
    vol_key TEXT(),
    fil_key INTEGER
    
       tbl_3 (type is 1)
    vol_key TEXT(),
    fil_key INTEGER
    
       tbl_4 (type is 2)
    vol_key TEXT(),
    fil_key INTEGER
    
       tbl_5 (type is 3)
    vol_key TEXT(),
    fil_key INTEGER
    right now, I've got the following

    Code:
    SELECT tbl_1.*, tbl_2.* FROM tbl_1,tbl_2
    WHERE
    (tbl_1.vol_key='some_string'
    AND
    (tbl_2.fil_key=tbl_1.fil_id
    AND
    tbl_2.vol_key='some string'))
    The above seems to work to get results from tbl_1 and tbl_2, but I also need to add the data from tbl_3 through tbl_5 when tbl_1.type is a 1, 2, or 3. I tried the CASE WHEN, but got an error near "CASE" message.

    My question is how would I code the conditional CASEs to pull in tbl_3 - tbl_5 if they're required while not breaking the query in tbl_1 and tbl_2 if the data from the latter tables aren't needed?

    Code:
    SELECT tbl_1.*, tbl_2.* FROM tbl_1, tbl_2,
    CASE WHEN tbl_1.type=1 THEN tbl_3.* END AS t_3,
    CASE WHEN tbl_1.type=2 THEN tbl_4.* END AS t_4,
    CASE WHEN tbl_1.type=3 THEN tbl_5.* END AS t_5
    FROM tbl_1, tbl_2 WHERE (...)
    I started with this, but it ended before it bagan with the error message near "CASE". I'm lost with this overly complicated exotic stuff. :D

    Any ideas would be greatly appricated. TIA
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Are the fields in the 3, 4, and 5 not the same type of data? If they aren't, then you should normalize your data and only have one table with the type as a field.


    If they are, then you need to outer join those tables.

    Comment

    • divideby0
      New Member
      • May 2012
      • 131

      #3
      Thank you for the reply and the link.

      I've saved the link and will mill over it to see which table method is best suited for what I'm trying to do.

      The tables do share common data types such as the "keys" but also have different fields for details specific to graphics, audio, or video.

      Comment

      Working...