简体   繁体   English

MSSQL中的等效SQL查询

[英]Equivalent SQL Query in MSSQL

Below query was successfully execute in MySQL but throwing an error in MSSQL. 下面的查询已在MySQL中成功执行,但在MSSQL中引发了错误。 Can anybody provide me an example to equivalent query in MSSQL 2008? 有人可以为我提供MSSQL 2008中等效查询的示例吗?

SELECT DISTINCT TOP 20 
  [users].[id], [users].[email], [users].[first_name], [users].[last_name], 
  (SELECT groups_groups.name 
   FROM users AS groups_users 
     LEFT JOIN users_groups AS groups_users_groups ON groups_users_groups.user_id = groups_users.id 
     LEFT JOIN groups AS groups_groups ON groups_groups.id = groups_users_groups.group_id 
   WHERE users.id = groups_users_groups.user_id) AS [groups] 
FROM [users] GROUP BY [users].[id], [users].[email], [users].[first_name], [users].[last_name] 
ORDER BY [users].[id] DESC

Error: 错误:

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression

SQL Server returns that error because your subquery is returning multiple values and it cannot assign multiple values to a user in a single record. SQL Server返回该错误,因为您的子查询返回多个值,并且它不能在单个记录中为用户分配多个值。

I think you can do the following: 我认为您可以执行以下操作:

  1. Review the logic of your subquery, in order that it will return just only one record per user. 查看子查询的逻辑,以便每个用户仅返回一条记录。
  2. Fix the data in order that your subquery will return only one record. 修复数据,以便子查询仅返回一条记录。

maybe your problem may be that the sub query is returning more than one result.. try 也许您的问题可能是子查询返回了多个结果。

SELECT DISTINCT TOP 20 
  [users].[id], [users].[email], [users].[first_name], [users].[last_name], 
  (SELECT TOP 1 groups_groups.name 
   FROM users AS groups_users 
     LEFT JOIN users_groups AS groups_users_groups ON groups_users_groups.user_id = groups_users.id 
     LEFT JOIN groups AS groups_groups ON groups_groups.id = groups_users_groups.group_id 
   WHERE users.id = groups_users_groups.user_id) AS [groups] 
FROM [users] GROUP BY [users].[id], [users].[email], [users].[first_name], [users].[last_name] 
ORDER BY [users].[id] DESC

顺便说一下,在MySQL中,该子查询可以简化为...

SELECT DISTINCT g.name FROM groups_groups g
   SELECT users.id as id ,groups_groups.name  into #groups
   FROM users AS groups_users 
   LEFT JOIN users_groups AS groups_users_groups 
       ON groups_users_groups.user_id = groups_users.id 
   LEFT JOIN groups AS groups_groups 
        ON groups_groups.id = groups_users_groups.group_id 
   WHERE users.id = groups_users_groups.user_id

  SELECT DISTINCT TOP 20 
  [users].[id], [users].[email], [users].[first_name], [users].[last_name], g.name
  FROM [users] 
  inner join #groups g  on users.id=g.id 
  ORDER BY [users].[id] DESC

this is not a tested code snippet,though from query mentioned in ques it looks like,if aim was to get all the groups for that set of user data(duplicating the user data but new group names)and was throwing error as sub query for single field returned multiple values ....if above scenario is the case getting all the user.id and groups.name into temp table based on joining required tables and then doing the inner join to combine all the user data with the group names based on the id should get the required data 这不是经过测试的代码段,尽管从问题中提到的查询看来,如果目的是获取该组用户数据的所有组(复制用户数据但使用新的组名),并且将错误作为子查询单个字段返回多个值....如果在上述情况下,基于联接所需表将所有user.id和groups.name放入临时表,然后进行内部联接以将所有用户数据与基于组名的表组合在一起在id上应该获得所需的数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM