[thelist] can / should this be done in SQL?

Edward McCarroll Ed at ComSimplicity.com
Thu Jul 9 00:22:31 CDT 2009


I don't know about MySQL, but I've actually used the following in
MS SQL Server:

  select top 2 * from ActivityOrganizer where OrganizerTypeId = 1
  union
  select top 3 * from ActivityOrganizer where OrganizerTypeId = 4
  union
  select top 5 * from ActivityOrganizer where OrganizerTypeId = 9
  order by ActivityId

The "order by" comes last because it applies to the entire union.

Regards,

  Ed

"Expletive Deleted!" (Richard M. Nixon)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Ed McCarroll                                     Ed at ComSimplicity.com
PO Box 654                                             (310) 904-3651
Culver City, CA 90232                           www.ComSimplicity.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  

> I tried:
> 
> SELECT * FROM news WHERE category = 1 ORDER BY date DESC LIMIT 4
> UNION
> SELECT * FROM news WHERE category = 2 ORDER BY date DESC LIMIT 3
> UNION
> SELECT * FROM news WHERE category = 6 ORDER BY date DESC LIMIT 3
> UNION
> SELECT * FROM news WHERE category = 3 ORDER BY date DESC LIMIT 2
> 
> Which gives me an error 'Incorrect usage of UNION and ORDER BY'




More information about the thelist mailing list