views:

156

answers:

4
+1  A: 

Try this:

SELECT to1.*
FROM t_order AS to1
WHERE
    to1.date IS NULL AND 
    to1.custId NOT IN (
     SELECT to2.custId
     FROM t_order AS to2
     WHERE to2.date IS NOT NULL
     GROUP BY to2.custId
    )
GROUP BY to1.custId

For MySQL 4:

SELECT to1.*
FROM t_order AS to1
LEFT JOIN t_order AS to2 ON
    to2.custId = to1.custId AND
    to2.date IS NOT NULL
WHERE
    to1.date IS NULL AND 
    to2.id IS NULL
GROUP BY to1.custId
glavić
I'm working with MYSQL4 which does not support SUBSELECTS.
prinzdezibel
I really don't know why I have earned -1 vote? Maybe because my code works in MySQL 5 and MySQL 4? very interesting...
glavić
I don't know either. maybe someone thinks he can do it in a simpler way. Well then, let's see your solution, Mister!
prinzdezibel
I know, someone didn't like my code styling, lol ;) Ah, all it really matters that you have your problem solved. Best regards.
glavić
+2  A: 

For a specific customer it's:

SELECT *
FROM t_order
WHERE date IS NULL AND custId=? LIMIT 1

For all customers its:

SELECT a.*
FROM t_order a 
    LEFT JOIN t_order b ON a.custId=b.custID and a.id != b.id
WHERE a.date IS NULL AND b.date IS NULL
GROUP BY custId;
sfossen
Wont work, both examples will return a customer even if the customer has a date in another row. The asker does not want this.
awithrow
ya.. just fixing now.. thanks.
sfossen
Does not work with modified data.
prinzdezibel
+1  A: 

This query will use one pass over index on custId.

For each distinct custId it will use one subquery over same index.

No GROUP BY, no TEMPORARY and no FILESORT — efficient, if your table is large.

SELECT VERSION()

--------
'4.1.22-standard'

CREATE INDEX ix_order_cust_id ON t_order(custId)

SELECT id, custId, order_date
FROM (
  SELECT o.*,
    CASE
      WHEN custId <> @c THEN
        (
        SELECT 1
        FROM   t_order oi
        WHERE  oi.custId = o.custId
          AND  order_date IS NOT NULL
        LIMIT 1
        )
    END AS n,
    @c <> custId AS f,
    @c := custId
  FROM
    (
    SELECT @c := -1
    ) r,
    t_order o
  ORDER BY custId
) oo
WHERE n IS NULL AND f

---------
1, 10, ''
5, 13, ''
Quassnoi
very intersting, even though I don't grasp it :) I would be interested how this solution performs in comparison with the others.
prinzdezibel
No GROUP BY, no TEMPORARY, no FILESORT -- how do you think it may perform? ;)
Quassnoi
A: 

First filter out rows with dates, then filter out any row that has a similar row with a lower id. This should work because the matching record with the least id is unique if id is unique.

select * from t_order o1
    where date is null
      and not exists (select * from t_order o2
                         where o2.date is null
                           and o1.custId = o2.custId
                           and o1.id > o2.id)
Jeffrey Hantin
First, he needs it for MySQL 4, so query without subselects must be used. Second, this query doesn't work like he want's it to work, read the question again. Third, why use id in condition ?
glavić