问题
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:
Look at this part:
WHERE () AND
()
is not a valid WHERE clause.
来源:https://stackoverflow.com/questions/3152469/syntax-error-in-automatically-formed-sql-query