Search code examples
mysqlquery-optimization

Syntax error on complex MySQL sub-queries


I can not see the problem here, unless I simply don't understand sub-queries in MySQL. I was given a slow query that had a lot of LEFT JOINs, so I was trying to re-write those JOINs as sub-queries. I started like this:

  select t.title AS title,
  (select 
    group_concat(distinct haha_supplier.display_name order by haha_supplier.display_name ASC separator ', ') AS contributors
    from haha_supplier where haha_supplier.supplier_id IN
               (select haha_title_to_supplier.supplier_id from haha_title_to_supplier where haha_title_to_supplier.title_id = t.title_id))
                  AS contributors,
  (select haha_supplier.supplier_id
    from haha_supplier where 
    where haha_supplier.supplier_id IN
               (select haha_title_to_supplier.supplier_id from haha_title_to_supplier where haha_title_to_supplier.title_id = t.title_id))
                 AS supplier_id,
  (select haha_supplier.group_letter
    from haha_supplier where 
    where haha_supplier.supplier_id IN
               (select haha_title_to_supplier.supplier_id from haha_title_to_supplier where haha_title_to_supplier.title_id = t.title_id))
                 AS group_letter,
  select 
         group_concat(distinct concat(user.first_name,' ',user.last_name) separator ', ') as marketer
         from user where user.id IN
                (select wawa_suppliers_to_haha_marketing_contacts.user_id from wawa_suppliers_to_haha_marketing_contacts
                where wawa_suppliers_to_haha_marketing_contacts.supplier_id IN
                  (select haha_supplier.supplier_id
                   where haha_supplier.supplier_id IN
                     (select haha_title_to_supplier.supplier_id from haha_title_to_supplier where haha_title_to_supplier.title_id = t.title_id)))
                         AS marketer,
  select 
         group_concat(distinct concat(user.first_name,' ',user.last_name) separator ', ') AS editor
         from user where user.id IN
                (select wawa_suppliers_to_haha_editors.user_id from wawa_suppliers_to_haha_editors
                where wawa_suppliers_to_haha_editors.supplier_id IN
                  (select haha_supplier.supplier_id
                   where haha_supplier.supplier_id IN
                     (select haha_title_to_supplier.supplier_id from haha_title_to_supplier where haha_title_to_supplier.title_id = t.title_id)))
                         AS editor,

I got:

  ERROR 1064 (42000): 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 '       from haha_supplier where haha_supplier.supplier_id IN
                 (select haha_title_to_supplier.supplier_id from haha_title_to_supplier where haha_title_to_supplier.title_id = t.title_id))'

What is the syntax error here?


Solution

  • select t.title AS title, (select group_concat(distinct haha_supplier.display_name order by haha_supplier.display_name ASC separator ', ') AS contributors from haha_supplier where haha_supplier.supplier_id IN ^----

    You're missing where somefield at the indicated spot...

    monday blindness... leaving this up as a personal hairshirt.