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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:42:43+00:00 2026-06-09T15:42:43+00:00

My head is spinning trying to figure out the SQL query I need to

  • 0

My head is spinning trying to figure out the SQL query I need to use. I have tried a variety of nested queries and self joins and everything is failing me. I assume I am WAY over thinking it.

Basic idea: I have a single table, let’s call it OrderTable and there are four (relevant) fields in the table: OrderId, CustomerId, RestaurantId, OrderTime

When a customer places an order, the date/time stamp is added to the OrderTime field. The other three fields are just integers, with the OrderId field as the primary key (and auto-increment).

I am trying to write a query that will return a list of CustomerId records where the first record (earliest date) for that customer is a specific date (let’s say ‘2012-03-14’) and the RestaurantId is a specific number (let’s say 29).

At the moment i have what i can only assume is an overly complicated way of doing it. Also, i currently get an error “#1242 – Subquery returns more than 1 row” when there is more than one record matching my subquery.

Can anyone help me with a more elegant solution? Thanks!

CURRENT QUERY:

SELECT `CustomerId` 
FROM `OrderTable` 
WHERE `OrderTime` LIKE '%2012-03-14%' 
AND `RestaurantId`='29' 
AND `OrderId`=(SELECT `OrderId` 
   FROM `OrderTable` 
   WHERE `RestaurantId`='29' 
   GROUP BY `CustomerId` 
   ORDER BY `OrderTime` ASC
   )
  • 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-09T15:42:45+00:00Added an answer on June 9, 2026 at 3:42 pm

    EDIT: John Totet Woo was probably right on this one, but still refer to the second part of my post to avoid the LIKE clause 🙂

    I might be slightly confused on what you’re asking for, but if you change the subquery from ‘=’ to IN, do you get what you’re after?

    SELECT `CustomerId` 
    FROM `OrderTable` 
    WHERE `OrderTime` LIKE '%2012-03-14%' 
    AND `RestaurantId`='29' 
    AND `OrderId` IN (SELECT `OrderId` 
        FROM `OrderTable` 
        WHERE `RestaurantId`='29' 
        GROUP BY `CustomerId` 
        ORDER BY `OrderTime` ASC
    ) 
    

    What was mostly bothering me though, is that you can use

    AND DATE(OrderTime) = '2012-03-14'
    

    Instead of the LIKE

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

Sidebar

Related Questions

My head is getting twisted trying to figure this out. The assignment is to
I'm banging my head on the keyboard trying to figure out a way to
I am just now starting to use version control and my head is spinning!
My head is spinning, I tried to do this on my own, but cant
My head is about to explode as I try to figure out the reason
I'm trying to run a MYSQL query where I need the same 'piece' of
Head scratching :( I am trying to bind one value to label and back
Even .head doesn't work. What changes do I need to make to make this
My head is about to explode with everything I've read about SQL Server and
I've spent a couple of hours looking at this and my head's spinning. Can

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.