Re: LIMIT in a subselect, but without using subselects



MrKrinkle wrote:
select * from shoppingcarts left join cartitems on cartitems.cartuid=
shoppingcarts.uid where shoppingcarts.uid in (select uid from
shoppingcarts order by created desc LIMIT 50,10);

How can I do that with a left join instead? thanks

I would do this with two separate queries:

1. select uid from shoppingcarts order by created desc LIMIT 50,10

2. Build a string that is the comma-separated list of uid's from the result of the first query, and interpolate it into the second query:

  select * from shoppingcarts left join cartitems
    on cartitems.cartuid = shoppingcarts.uid
  where shoppingcarts.uid in ( $LIST_OF_UIDS )

Be careful to test if the list is empty, because "IN ( )" is a syntax error in SQL.

There might be a clever way to use outer joins to do this in a single query (along the lines of Nis' solution to the thread "Limit the number of a left join" a couple of weeks ago on this newsgroup). But the extra work it takes to code, test, and maintain such clever queries is usually too great to justify the slight benefit from doing the query in one step.

Sometimes it is more straightforward to just do a query in two steps, and move on to the next problem in your project. Also, keep in mind that someday, someone with only modest skill in SQL might have to maintain your code, and using very sophisticated queries can make it harder for that person to understand it.

Regards,
Bill K.
.



Relevant Pages

  • Re: Problems with Delete Command
    ... The SQL could get fairly messy if you need to construct it in code, ... ContactID, and WebComID, and create your on-the-fly SQL on that saved query, ... to find the list of ContactIDs from the junction table, ... This is a style/readability thing: if you are going to use string ...
    (microsoft.public.access.tablesdbdesign)
  • Re: Need help with Code Please!!!
    ... posted in response to my last post was some SQL, but this is not the SQL that ... the actual string that gets built at the end of the SQ1 build process. ... The message says Syntax error in query expression ... Dim rs As Recordset 'object ref to qryCompany\USFNumber ...
    (microsoft.public.access.formscoding)
  • Re: Requery subform
    ... are just changing the SQL? ... remove this function and relevent code because the query always exists. ... Dim strProjectType As String ... Dim strProjectStatus As String ...
    (comp.databases.ms-access)
  • Re: Building SQL statement in VBA. Getting 3131 Runtime Error in FROM clause :@
    ... Number of characters in a cell in the query design grid 1,024 ... Since the SQL parser mentions an error in the FROM clause, ... The only problem I can see is the SQL string is very long. ... Dim qdf As QueryDef ...
    (microsoft.public.access.queries)
  • RE: Jet/Query editor destroys query...
    ... Oh, just to clarify, the auto-generated name for the sub-query was: ... Interestingly, my original SQL: ... within a double-quoted string literal. ... If I simply saved the query string (by directly assigning the string to the ...
    (microsoft.public.access.queries)