Question

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
   )
Was it helpful?

Solution

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

OTHER TIPS

I guess I'm confused. Why can't you just select all the records that equal your OrderTime and Restaurant ID like this?

SELECT * FROM OrderTable WHERE OrderTime = "2012-03-14" AND RestaurantID = "29";

Would that not give you the list you want?

Your current can return multiple values. The = sign need only one value from your subquery and in order to do that, you need to limit the values of your subquery by using the LIMIT keyword.

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

you can also do it this way:

SELECT DISTINCT CustomerID
FROM OrderTable
WHERE OrderID IN
(
    SELECT  OrderID
    FROM    OrderTable 
    WHERE   RestaurantID = 29 AND
            DATE(OrderTime) = DATE(2012-03-14)
) b

the simpliest solution among all is this:

    SELECT  DISTINCT CustomerID
    FROM    OrderTable 
    WHERE   RestaurantID = 29 AND
            DATE(OrderTime) = DATE(2012-03-14)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top