Search code examples
mysqlsqlleft-joinlimit

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


so i had to adjust a query i used because the limit at the end of the table would cause the entire first table to be read before limiting. this resulted in a timeout from my mysql server. now i created the query like i read in another post on stack overflow and came up with this:

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

This was my original query which resulted in a timeout:

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

Does anyone know what is causing the error? Or someone that knows how to correct my original query?


Solution

  • Fixed it thanks to @Ravinder his comment about missing the on clause updated 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