Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8459161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:15:55+00:00 2026-06-10T13:15:55+00:00

This query gets me the results I want from one primary table m ,

  • 0

This query gets me the results I want from one primary table m, three secondary tables s, o, and d with help of three lookup tables ls, lo and ld, but it queries every table once for every field.

SELECT m.id, 
(SELECT GROUP_CONCAT(s.field1 SEPARATOR ', ')
    FROM s, ls
    WHERE ls.mid=m.id AND ls.sid=s.id) 
    AS s_field,
(SELECT GROUP_CONCAT(o.field1 SEPARATOR ', ')
    FROM o, lo
    WHERE lo.mid=m.id AND lo.oid=o.id) 
    AS o_field1,
(SELECT GROUP_CONCAT(o.field2 SEPARATOR ', ')
    FROM o, lo
    WHERE lo.mid=m.id AND lo.oid=o.id) 
    AS o_field2,
(SELECT GROUP_CONCAT(o.field3 SEPARATOR ', ')
    FROM o, lo
    WHERE lo.mid=m.id AND lo.oid=o.id) 
    AS o_field3,
(SELECT GROUP_CONCAT(o.field4 SEPARATOR ', ')
    FROM o, lo
    WHERE lo.mid=m.id AND lo.oid=o.id) 
    AS o_field4,
(SELECT GROUP_CONCAT(d.field1 SEPARATOR ', ')
    FROM d, ld
    WHERE ld.mid=m.id AND ld.did=d.id) 
    AS d_field1,
(SELECT GROUP_CONCAT(d.field2 SEPARATOR ', ')
    FROM d, ld
    WHERE ld.mid=m.id AND ld.did=d.id) 
    AS d_field2
FROM m
WHERE m.id=22 GROUP BY m.id

How can I rewrite it to get the same results but with only one subquery per table.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T13:15:56+00:00Added an answer on June 10, 2026 at 1:15 pm

    You can use the following:

    SELECT m.id, 
        GROUP_CONCAT(sls.field1 SEPARATOR ', ') AS s_field,
        GROUP_CONCAT(olo.field1 SEPARATOR ', ') AS o_field1,
        GROUP_CONCAT(olo.field2 SEPARATOR ', ') AS o_field2,
        GROUP_CONCAT(olo.field3 SEPARATOR ', ') AS o_field3,
        GROUP_CONCAT(olo.field4 SEPARATOR ', ') AS o_field4,
        GROUP_CONCAT(dld.field1 SEPARATOR ', ') AS d_field1,
        GROUP_CONCAT(dld.field2 SEPARATOR ', ') AS d_field2,
    FROM m
    (
        SELECT ls.mid, s.field1
        FROM s
        INNER JOIN ls
            on s.id = ls.sid
    ) sls
        on m.id = sls.mid
    INNER JOIN
    (
        SELECT lo.mid, o.field1, o.field2, o.field3, o.field4
        FROM o
        INNER JOIN lo
            on o.id = lo.oid
    ) olo
        on m.id = olo.mid
    INNER JOIN
    (
        SELECT ld.mid, d.field1, d.field2
        FROM d
        INNER JOIN ld
            on d.id = ld.did
    ) dld
        on m.id = dld.mid
    WHERE m.id=22 
    GROUP BY m.id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following query gets all the results from a table called video SELECT video.*
I have one mysql query, which gets rows to designs list. In this list
I have a table that I want to pick one row from it and
I have an insert query that gets generated like this INSERT INTO InvoiceDetail (LegacyId,InvoiceId,DetailTypeId,Fee,FeeTax,Investigatorid,SalespersonId,CreateDate,CreatedById,IsChargeBack,Expense,RepoAgentId,PayeeName,ExpensePaymentId,AdjustDetailId)
I have a complex query that gets executed like this: if ($stmt = $dbi->prepare($pt_query))
this query SELECT * FROM tblContracts LEFT JOIN tblPartys ON tblContracts.id = tblPartys.Contract_id INNER
This query is somewhat related to this earlier one on sorting where, it was
This query works fine for me: $query = SELECT p.topnode_id, p.param_key, p.param_value FROM tbl_params
This query works: item = db.GqlQuery(SELECT * FROM Item WHERE CSIN = 13)[0] although
I have a data access page which gets all it's data from a query

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.