MySql到MS Access查询转换

时间:2018-06-03 20:04:02

标签: mysql ms-access

我有一个mysql查询,我想在MS Access中使用它。

SELECT company_name, agent_id FROM
( SELECT company_name, agent_id, @rn := IF(@prev = agent_id, @rn + 1, 1) 
  AS rn, @prev := agent_id FROM users 
  JOIN (SELECT @prev := NULL, @rn := 0) AS vars 
  ORDER BY agent_id DESC, company_name)
 AS T1 
 WHERE T1.agent_id is not null and  rn <= 3;

我一直在尝试在Access中运行它,但它抛出了错误:&#34; from子句&#34;中的语法错误。

1 个答案:

答案 0 :(得分:0)

在Microsoft Access中,我们可以尝试使用&#34; TOP n&#34; WHERE子句中的相关子查询。

我尝试这样的事情:

 SELECT t.company_name
      , t.agent_id
   FROM [users] AS [t]
  WHERE t.company_name IN
        ( SELECT TOP 3 u.company_name
            FROM [users] AS [u]
           WHERE u.agent_id = t.agent_id
           ORDER
              BY u.company_name
        )
  ORDER
     BY t.agent_id DESC
      , t.company_name