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

  • Home
  • SEARCH
  • 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 6933625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:51:23+00:00 2026-05-27T11:51:23+00:00

This is a question regarding queries in mysql. As an example let’s use a

  • 0

This is a question regarding queries in mysql. As an example let’s use a system that tracks the selling of cars. There is table with salespersons, a table of sales and a table that links the two. Most of the time each sale has only one person associated with it. But sometimes multiple persons are associated with a sale.

Table sales
id | carId | date
---+-------+-----------
1  | 2     | 11-01-2011
2  | 8     | 11-02-2011
3  | 5     | 11-05-2011

Table link
personId | saleId
---------+-------
2        | 1 
2        | 2 
1        | 3
2        | 3

Table salesperson
id | name  | age
---+-------+----
1  | Barry | 25
2  | Sara  | 32

To retrieve the data I want to show the information of each sale in a webpage. But when multiple persons are associated with a sale it becomes problematic. I have tried applying the query below in my code, after which the PHP handles the exception of when multiple persons are associated with one sale, but due to the length of the sales table the query below takes too much time to run:

SELECT count(personId) as amount, carId, date
FROM link 
JOIN salesperson ON link.personId = salesperson.id  
JOIN sales ON sales.id = link.saleId
GROUP BY link.saleId

Is there a more efficient query that can be used? By the way indexing of the table is already done.

  • 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-05-27T11:51:24+00:00Added an answer on May 27, 2026 at 11:51 am

    Then it is just:

    SELECT STRAIGHT_JOIN
        amount, carId, date
    FROM (
        SELECT saleId, count(*) as amount
        FROM link
        GROUP BY saleId
        ORDER BY NULL) as amounts
    JOIN sales ON sales.id = amounts.saleId;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got few doubts regarding quoting mysql queries in Zend framework. Though this question
Yesterday, I asked this question regarding best practices for a simple information retrieval system
First of all there is a partial question regarding this, but it is not
This is a very specific question regarding MySQL as implemented in WordPress . I'm
This question is regarding the ASP.NET webservice that i am creating using the DAL-BLL
I've a question regarding MYSQL queries, and how I could go about handling getting
This is a question regarding the undocumented limits that Twitter will impose for overusing
Yesterday I posted this question regarding using lambdas inside of a Join() method to
This question is regarding getopt function in php. I need to pass two parameter
This is a question regarding Unix shell scripting (any shell), but any other "standard"

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.