views:

21

answers:

1

I am trying to generate a query for use with a smart search field, ala google suggest.

I can not form the query directly, although I can see what query was made via the logs.

I have posted the query that gets sent, which has a syntax error but I am unsure why.

SELECT DISTINCT IF(guests.passport != '', Concat(guests.passport,
                                                          IF(
                                          guests.lastname !=
                                          ''
                                                   OR guests.passport!= '',
                                                          Concat
                                                          (' (',
                                                          IF(guests.lastname !=
                                                             '',
                                                          guests.lastname,
                                                          '{blank}')
                                                          , ', ',
                                                          IF(
                                                          guests.firstname != ''
                                                             , guests.passport,
                                                          '{blank}')
                                                             , ')'), '')),
                                IF(guests.lastname != ''
                    OR guests.passport!= '', Concat(IF(guests.lastname != '',
                                                    guests.lastname,
                                                    '{blank}'), ', ',
                                                                          IF(
                                             guests.passport!= '',
                                             guests.passport, '{blank}')),
                                ''
                ))              AS display,
                `guests`.`uuid` AS VALUE,
                as              secondary,
                guests.id       AS classname
FROM   guests
WHERE  ( guests.id = 0 )
       AND ( ( guests.firstname LIKE 'b%'
                OR guests.lastname LIKE 'b%'
                OR guests.passport LIKE 'b%' )
              OR (( guests.firstname LIKE 'b%'
                     OR guests.firstname LIKE '% b%'
                     OR guests.lastname LIKE 'b%'
                     OR guests.lastname LIKE '% b%'
                     OR guests.passport LIKE 'b%'
                     OR guests.passport LIKE '% b%' )) )
ORDER  BY IF(guests.passport != '', Concat(guests.passport,
                                              IF(guests.lastname != ''
                                                                OR
                                                 guests.passport!= '',
                                              Concat
                                              (' (',
                                              IF(guests.lastname != '',
                                              guests.lastname, '{blank}')
                                              , ', ',
                                                                        IF(
                                              guests.firstname != ''
                                                       , guests.passport,
                                              '{blank}')
                                                       , ')'), '')),
                    IF(guests.lastname != ''
              OR guests.passport!= '', Concat(IF(guests.lastname != '',
                                              guests.lastname,
                                                                       '{blank}'
                                              ), ', ',
                                       IF(
                                       guests.passport!= '', guests.passport,
                                       '{blank}'
                                       )),
                    ''
          ))
LIMIT  0, 8  

Is there any glaring error in that query?

+1  A: 

Look at this part:

WHERE () AND

() is not a valid WHERE clause.

RedFilter
Ahh, thankyou that was it. I have remidied that howeverm and there is still a syntax error. I edited my question to avoid posting a new one, but let me know if I should revert and just mark you as the answer to my original question
Jacob