Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. SQLite query

SQLite query

Scheduled Pinned Locked Moved Database
databasesqlite
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    David Crow
    wrote on last edited by
    #1

    I have the following three tables:

    TABLE_A TABLE_B TABLE_C


    id (primary) tablea_id (foreign) tablea_id (foreign)
    name boards text
    date
    phone

    I have the following query to select all rows from TABLE_A and also count how many related rows are in each of the other two tables:

    SELECT *, (SELECT COUNT(TABLE_B.tablea_id) FROM TABLE_B WHERE TABLE_B.tablea_id == TABLE_A.id) AS Cnt1, (SELECT COUNT(TABLE_C.tablea_id) FROM TABLE_C WHERE TABLE_C.tablea_id == TABLE_A.id) AS Cnt2 FROM TABLE_A

    This works fine, but I was curious if: 1) SQL was doing three separate queries behind the scene, and 2) a more efficient way exists. Thank you. - DC

    "One man's wage rise is another man's price increase." - Harold Wilson

    "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

    "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

    M 1 Reply Last reply
    0
    • D David Crow

      I have the following three tables:

      TABLE_A TABLE_B TABLE_C


      id (primary) tablea_id (foreign) tablea_id (foreign)
      name boards text
      date
      phone

      I have the following query to select all rows from TABLE_A and also count how many related rows are in each of the other two tables:

      SELECT *, (SELECT COUNT(TABLE_B.tablea_id) FROM TABLE_B WHERE TABLE_B.tablea_id == TABLE_A.id) AS Cnt1, (SELECT COUNT(TABLE_C.tablea_id) FROM TABLE_C WHERE TABLE_C.tablea_id == TABLE_A.id) AS Cnt2 FROM TABLE_A

      This works fine, but I was curious if: 1) SQL was doing three separate queries behind the scene, and 2) a more efficient way exists. Thank you. - DC

      "One man's wage rise is another man's price increase." - Harold Wilson

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      If you turn on the Execution Plan SSMS will show you the 3 queries and their relative costs. I often use this tool to compare the performance of queries constructed differently to do the same job. I would use 3 union queries but I'd bet the cost is identical on such a simple query. Sorry I missed the SQL Lite platform in the title :-O

      Never underestimate the power of human stupidity RAH

      D 1 Reply Last reply
      0
      • M Mycroft Holmes

        If you turn on the Execution Plan SSMS will show you the 3 queries and their relative costs. I often use this tool to compare the performance of queries constructed differently to do the same job. I would use 3 union queries but I'd bet the cost is identical on such a simple query. Sorry I missed the SQL Lite platform in the title :-O

        Never underestimate the power of human stupidity RAH

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        Mycroft Holmes wrote:

        Sorry I missed the SQL Lite platform in the title :O

        That's okay. I could've qualified it further by stating it's running on an Android platform.

        "One man's wage rise is another man's price increase." - Harold Wilson

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups