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 8444449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:24:03+00:00 2026-06-10T09:24:03+00:00

I have 3 tables in a MySQL database. Table 1 (say it’s called client

  • 0

I have 3 tables in a MySQL database.

Table 1 (say it’s called client) has information of a client: client_id and client_name
Table 2 (say it’s called user_to_client) has information related to each client with the users of my system: user_id, client_id and order_flag.

In this table clients and users are repeated, order flag indicates that the user and client have unfinished business. A combination of user_id and client_id can only be found once in that table, it means user 5 and client 3 is only registered one time.

Table 3 (called orders) keeps information about the basic information of an order: client_id, user_id, order_id. It keeps two different date type columns: date_started which keeps information of the date when they started to build the order and date_sent which keeps the date when the order was sent to the operations area.

The reason I repeat the information in two tables is because in table 3, user and client can have a ton of unfinished orders which are no longer available, but i need to keep for the company records.

So, what i need to do is select the 4 last clients with unfinished orders without repeating the clients, and the id to that last unfinished order. Basically i need to select from table 1 the client name, if that client has in table 2 order_flag activated (value 1) and from table 3 the latest order_id which belongs to that user.

I cannot modify the tables because they are being used for another job already, so, can someone help me with a MySQL query to accomplish the job?

  • 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-10T09:24:05+00:00Added an answer on June 10, 2026 at 9:24 am

    The following query does not query answer your question, because clients can be repeated. However, it provides a good basis for figuring out what to do:

    select c.*, o.*
    from client c join
         user_to_client uc
         on c.client_id = uc.client_id join
         orders o
         on o.client_id = uc.client_id and
            o.user_id = uc.user_id
    where uc.order_flag = 1 and
          o.date_sent is null  -- does this make an order "unfinished"?
    order by date_started desc
    limit 4
    

    I’m not sure how you find “unfinished” orders. So, I’m guessing that it is based on date_sent being NULL.

    What you actually need is a bit more complicated. You want the most recent “unfinished” order for each client. The following query uses a correlated subquery to get this information:

    select client_id, maxdate,
           (select max(order_id)
            from orders o
            where o.client_id = c.client_id and o.date_sent is null and o.date_started = c.maxdate) as orderid
    from (select c.client_id, MAX(date_started) as maxdate
          from client c join
               user_to_client uc
               on c.client_id = uc.client_id join
               orders o
               on o.client_id = uc.client_id and
                  o.user_id = uc.user_id
          where uc.order_flag = 1 and
                o.date_sent is null
          group by c.client_id
          order by 2 desc
          limit 4
         ) c
    

    This makes the assumption that it can find the right order based on the client id and date. If this is not true, then the correlated subquery has to bring in the other tables to get the right filtering information.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a MySQL database with 3 tables: table 1: Persons, with
I have multiple tables in a MySQL database. Lets say they look like this:
Let's say we have system A comprising a MySQL database, with several tables. After
I have 2 tables in my mysql database: CREATE TABLE IF NOT EXISTS `RECIPES`
In a MySQL database I have two tables linked in a join. One table
I have the following two tables in my mysql database. Table Name: groups id
What I have is two tables inside of a mysql database. One table contains
I have three mysql table from same database Db1. Three tables have following columns.
I have a MySQL database table called Participant that looks something like this: (idParticipant)
I have this MySQl database that has a table which contains a column that

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.