简体   繁体   English

尝试退出联接时出现意外结果

[英]Unexpected results when trying to left join

I have this SQL statement which works fine.. 我有这个SQL语句,可以正常工作..

SELECT result=count(j.client_id), j.client_id 
FROM spp_job j  
GROUP BY j.client_id 
ORDER BY  count(j.client_id) DESC

but failed when I use LEFT JOIN 但是当我使用LEFT JOIN时失败

SELECT result=count(j.client_id), c.name 
FROM spp_job j 
LEFT JOIN spp_client c ON j.client_id = c.id  
GROUP BY j.client_id 
ORDER BY count(j.client_id) DESC

I don't know what went wrong. 我不知道出了什么问题。 I want to show the client name instead of their id. 我想显示客户端名称而不是其ID。 The second SQL give me all client records. 第二条SQL给我所有的客户记录。

Since you are listing c.name with the left join, you need to Group By c.name instead j.client_id 由于要使用左联接列出c.name,因此需要Group By c.name instead j.client_id

SELECT result=count(j.client_id), c.name 
FROM spp_job j LEFT JOIN
    spp_client c ON j.client_id = c.id  
GROUP BY c.name 
ORDER BY result DESC

UPDATE: Since name is not unique (as @lc. said) you may need to use group by both by id and name for accurate results. 更新:由于名称不是唯一的(如@lc。所说),您可能需要同时使用id和名称的group by来获得准确的结果。

GROUP BY j.client_id, c.name 
ORDER BY result DESC

I think you want to get the NAME for every client_id . 我认为您想为每个client_id获取NAME

you also need to add name in the GROUP BY clause because the column is not aggregated , 您还需要在GROUP BY子句中添加name ,因为该列未聚合

SELECT  result = count(j.client_id), 
        c.name 
FROM    spp_job j 
        LEFT JOIN spp_client c 
            ON j.client_id = c.id  
GROUP   BY j.client_id, c.name
ORDER   BY result DESC

You are running into a Sybase extension to the standard behavior of GROUP BY . 您正在对GROUP BY的标准行为进行Sybase扩展。 From the documentation (see halfway down the page at "Transact-SQL extensions to group by and having"): 文档中 (请参阅“中间的Transact-SQL扩展进行分组并具有”的页面的一半):

Transact-SQL extensions to standard SQL make displaying data more flexible, by allowing references to columns and expressions that are not used for creating groups or summary calculations: Transact-SQL对标准SQL的扩展通过允许引用不用于创建组或汇总计算的列和表达式,使显示数据更加灵活:

  • A select list that includes aggregates can include extended columns that are not arguments of aggregate functions and are not included in the group by clause. 包含聚合的选择列表可以包含扩展列,这些扩展列不是聚合函数的参数,也不包含在group by子句中。 An extended column affects the display of final results, since additional rows are displayed. 扩展列会影响最终结果的显示,因为会显示其他行。

    • The group by clause can include columns or expressions that are not in the select list. group by子句可以包括不在选择列表中的列或表达式。

... ...

When the Transact-SQL extensions add rows and columns to a display, or if group by is omitted, query results may be hard to interpret. 当Transact-SQL扩展将行和列添加到显示时,或者如果省略group by,则查询结果可能难以解释。

The example from that page explains what happens: 该页面上的示例说明了发生的情况:

The Transact-SQL extended column, price (in the select list, but not an aggregate and not in the group by clause), causes all qualified rows to display in each qualified group, even though a standard group by clause produces a single row per group. Transact-SQL扩展列,价格(在选择列表中,但不是集合,而不在group by子句中),即使所有标准group by子句在每个合格组中仅显示一行,它也会在所有合格组中显示所有合格行组。 The group by still affects the vector aggregate, which computes the average price per group displayed on each row of each group (they are the same values that were computed for example a) 分组依据仍然会影响矢量集合,该向量将计算每组每行显示的每组平均价格(例如,它们的计算值相同)

Note that the standard behavior of GROUP BY does not allow this and will produce an error to the effect of "Use of non-aggregated column not specified in group-by clause". 请注意, GROUP BY的标准行为不允许这样做,并且会产生错误,即“使用group-by子句中未指定的非聚合列”。


For your query, you should also group by c.name as JW. 对于查询,您还应该按c.name 为JW。 suggests . 建议

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

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