|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Record count should be null. Why 1?
I have a table that contains, among other things, customerid and orderid.
I need to get the last order that a customer made, if any. It's quite possible that they have never ordered. I'm using the following SQL: select max(orderid) as lastOrder from orders where customerid='#userid#'; If the customerid isn't in the table (never ordered before), I would expect a record count of 0 or null. However, I'm getting a record count of 1 with a value of [empty string] in lastOrder. What gives? I need to run another query if the customer has never ordered, but the other query isn't being run because the first one is finding a result. Any ideas how I can do this differently or better? Thanks, G |
|
#2
|
|||
|
|||
|
Max and Min will return one row regards. You should try using count instead.
__________________
El éxito consiste en una serie de pequeñas victorias día a día MySQL, MS SQL, MS ACCESS, Oracle Database Manager - http://victorpendleton.net/products/psdviewer.html |
|
#3
|
||||
|
||||
|
The following should return a single record with the maximum orderid or an empty recordset:
SELECT TOP 1 orderid AS lastOrder FROM orders WHERE customerid='#userid#' ORDER BY orderid DESC |
|
#4
|
|||
|
|||
|
Ok. I like that solution.
My current solution is to test the result to make sure it's an INT, set a boolean, and switch on that. It'll save a couple steps to just have a proper query. Thanks all who answered! |
![]() |
| Viewing: Dev Shed Forums > Databases > MS SQL Development > Record count should be null. Why 1? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|