MySQL 查询有多个连接并限制第一个 table 时出错

Error in MySQL Query with multiple joins and limit on first table

所以我不得不调整我使用的查询,因为 table 末尾的限制会导致整个第一个 table 在限制之前被读取。这导致我的 mysql 服务器超时。现在我创建了查询,就像我在另一个关于堆栈溢出的 post 中读到的那样,并想出了这个:

SELECT a.title
     , a.lat
     , a.lon
     , a.a_content_id
     , a.date_added
     , r.countRep
     , i.countInt
     , content.img
     , c.marker
     , c.subcatvan 
  FROM 
     ( SELECT title
            , lat
            , lon
            , alert_content_id
            , date_added
            , cat 
         FROM alerts 
        LIMIT 10
     ) a
  LEFT 
  JOIN 
     ( SELECT COUNT(DISTINCT id) countRep 
         FROM reply 
        WHERE alert_id = alerts.alerts
     ) r
  LEFT 
  JOIN 
     ( SELECT COUNT(DISTINCT id) countInt 
         FROM interactions  
        WHERE alert_id = alerts.alerts
     ) i
  LEFT 
  JOIN 
     ( SELECT img 
         FROM alerts_content 
        WHERE alert_id = alerts.alerts
     ) content
  LEFT 
  JOIN 
     ( SELECT marker
            , subcatvan 
         FROM categories 
        WHERE a.cat = id
     ) c;
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 45

这是导致超时的原始查询:

SELECT
a.title, a.lat, a.lon, a.alert_content_id, a.date_added, count(DISTINCT r.id) as countRep ,count(DISTINCT i.id) AS countInt,
ac.img, c.marker, c.subcatvan
FROM `alerts` a
LEFT JOIN
`reply` r ON r.alert_id = a.alerts
LEFT JOIN
`interactions` i ON i.alert_id = a.alerts
LEFT JOIN
`alerts_content` ac ON ac.alert_id = a.alerts
LEFT JOIN
`categories` c ON a.cat = c.id
GROUP BY a.title, a.lat, a.lon, a.alert_content_id, a.date_added LIMIT 0,10

有谁知道是什么导致了这个错误? 或者知道如何更正我的原始查询的人?

尝试在 where 中将 table 名称添加到 ID。

c.id 而不是 id

您需要修复子查询。他们需要使用 group by 而不是相关:

SELECT a.title, a.lat, a.lon, a.a_content_id, a.date_added, r.countRep,
       i.countInt, content.img, c.marker, c.subcatvan 
FROM (SELECT title, lat, lon, alert_content_id, date_added, cat
      FROM `alerts`
      LIMIT 10
     ) a LEFT JOIN
     (SELECT alert_id, count(DISTINCT id) as countRep
      FROM `reply`
      GROUP BY alert_id
     ) r
     ON r.alert_id = a.alerts r LEFT JOIN
     (SELECT alert_id, count(DISTINCT id) AS countInt
      FROM `interactions`
      GROUP BY alert_id
     ) i
     ON i alert_id = a.alerts LEFT JOIN
     (SELECT alert_id, img
      FROM `alerts_content`
      GROUP BY alert_id
     ) content
     ON alert_id = a.alerts LEFT JOIN
     `categories` c
     ON a.cat = c.id;

感谢@Ravinder 他关于缺少 on 子句的评论修复了它 已更新 sql:

SELECT
        a.alerts, a.title, a.lat, a.lon, a.alert_content_id, a.date_added, r.countRep, i.countInt, 
        ac.img, c.marker, c.subcatvan 
    FROM 
        (SELECT alerts, title, lat, lon, alert_content_id, date_added, cat 
            FROM `alerts` LIMIT 10) a
    LEFT JOIN 
        (SELECT alert_id,count(DISTINCT id) as countRep 
            FROM `reply`) r 
        ON r.alert_id = a.alerts
    LEFT JOIN 
        (SELECT alert_id,count(DISTINCT id) AS countInt 
            FROM `interactions`) i 
        ON i.alert_id = a.alerts
    LEFT JOIN 
        (SELECT alert_id, img 
            FROM `alerts_content`) ac 
        ON ac.alert_id = a.alerts
    LEFT JOIN 
        (SELECT id, marker, subcatvan 
            FROM `categories`) c 
        ON a.cat = c.id
    GROUP BY a.title, a.lat, a.lon, a.alert_content_id, a.date_added