Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

subquery in FROM returning error, MySQL

New Here ,
Feb 05, 2011 Feb 05, 2011

Hi,

I am trying to order by date first (descending), then group by and order by last name. Tried just order by and group by, but didn't work. I'm now trying to get there through a subquery, the subquery works as a query on it's own, but I'm running into syntax errors when I make it a subquery. Here's my code:

SELECT * FROM (SELECT clientrogue_info.id_clientrogue AS idrogue, clientrogue_info.roguefirst AS first, clientrogue_info.roguelast AS last, provider_cvpv.idprovidercvpv AS cvpvid, provider_cvpv.providerid AS providerid FROM clientrogue_info LEFT JOIN provider_cvpv on provider_cvpv.cpid = clientrogue_info.id_clientrogue
ORDER BY visitdate DESC) AS t1 GROUP BY t1.last ORDER BY t1.last

Thanks in advance for help.

TOPICS
Server side applications
389
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Feb 05, 2011 Feb 05, 2011

That's not really a subquery, it's a derived table. In most DBMS's, the main select list must reference the derived table's alias.There may be other problems but that's the first thing to jump out at me.

Try:

SELECT t1.* FROM (SELECT clientrogue_info.id_clientrogue AS idrogue,  clientrogue_info.roguefirst AS first, clientrogue_info.roguelast AS  last, provider_cvpv.idprovidercvpv AS cvpvid, provider_cvpv.providerid  AS providerid FROM clientrogue_info LEFT JOIN provider_cvpv on  provider_cvpv.cpid

...
Translate
LEGEND ,
Feb 05, 2011 Feb 05, 2011

That's not really a subquery, it's a derived table. In most DBMS's, the main select list must reference the derived table's alias.There may be other problems but that's the first thing to jump out at me.

Try:

SELECT t1.* FROM (SELECT clientrogue_info.id_clientrogue AS idrogue,  clientrogue_info.roguefirst AS first, clientrogue_info.roguelast AS  last, provider_cvpv.idprovidercvpv AS cvpvid, provider_cvpv.providerid  AS providerid FROM clientrogue_info LEFT JOIN provider_cvpv on  provider_cvpv.cpid = clientrogue_info.id_clientrogue
ORDER BY visitdate DESC) AS t1 GROUP BY t1.last ORDER BY t1.last

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 06, 2011 Feb 06, 2011
LATEST

Thanks for the info on derived tables bregent,

That did the trick, I hadn't used an alias for the table. It works exactly like I want it too!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines