2

我正在尝试在我的 sql 查询中执行几个 JOINS,但遇到了问题。我需要做的是从poker_sites表中选择所有字段,然后从中获取 2 个相关字段networks,如果可用,则从中获取平均评分editor_content

我遇到的问题是查询只返回一行,而应该至少有三行。

任何帮助将非常感激。

这是我的 SQL

SELECT AVG(editor_content.rating) AS rating, poker_sites.*,
networks.network_name, networks.network_icon FROM poker_sites
    LEFT JOIN networks
    ON (poker_sites.network_id=networks.network_id)
    LEFT JOIN editor_content
    ON (poker_sites.site_id=editor_content.assign_id)
    WHERE poker_sites.published=1
4

1 回答 1

4

如果要使用聚合函数(在本例中为 AVG)获得多个结果,则需要 GROUP BY。

    SELECT x.avgRating AS rating, poker_sites.*,
        networks.network_name, networks.network_icon 
    FROM poker_sites
    LEFT JOIN networks
        ON (poker_sites.network_id=networks.network_id)
    LEFT JOIN 
    (
        SELECT AVG(editor_content.rating) as avgRating, editor_content.assign_id
        FROM editor_content
        GROUP BY editor_content.assign_id
    ) x
        ON (poker_sites.site_id = x.assign_id)
    WHERE poker_sites.published=1
于 2012-09-15T01:21:47.070 回答