自动生成sql查询的麻烦

时间:2010-06-30 19:45:35

标签: sql database mysql

我遇到了自动生成的SQL查询的问题,该查询基于存储在表中的几个较小的查询,并与用户输入的内容相结合。

我将显示有效查询的输入,然后显示查询本身。

然后我会显示我的输入,以及失败的查询。

如果有人可以指出我使用了哪些输入导致我的查询失败,我将非常感激。

fromclause:

((`clients` INNER JOIN `addresstorecord` 
    ON `clients`.`uuid` = `addresstorecord`.`recordid` 
    AND `addresstorecord`.`tabledefid`='tbld:6d290174-8b73-e199-fe6c-bcf3d4b61083' 
    AND addresstorecord.primary='1') 
INNER JOIN `addresses` ON `addresstorecord`.`addressid` = `addresses`.`uuid`)

displayfield:

IF(clients.company != '', 
   CONCAT(clients.company,
          IF(clients.lastname != '' OR clients.firstname != '', 
             CONCAT(' (',
                    IF(clients.lastname != '', clients.lastname, '{blank}'),
                    ', ',
                    IF(clients.firstname != '', clients.firstname, '{blank}'),
                    ')'),
             '')
          ),
   IF(clients.lastname != '' OR clients.firstname != '',
      CONCAT(IF(clients.lastname != '', clients.lastname, '{blank}'),
             ', ',
             IF(clients.firstname != '', clients.firstname, '{blank}')),
      ''))
)

secondaryfield:

IF(addresses.city != '' OR addresses.state !='' OR addresses.postalcode != '',
  CONCAT(IF(addresses.city != '', addresses.city, ''),
         ', ',
         IF(addresses.state != '', addresses.state, ''),
         ' ',
         IF(addresses.postalcode != '', addresses.postalcode, '')),
  'unspecified location')

classfield:

clients.type

searchfields:

clients.company, clients.firstname, clients.lastname

filterclause:

clients.inactive=0

有效的查询:

SELECT
                COUNT(IF(clients.company != '', CONCAT(clients.company,IF(clients.lastname != '' OR clients.firstname != '', CONCAT(' (',if(clients.lastname != '', clients.lastname, '{blank}'),', ',if(clients.firstname != '', clients.firstname, '{blank}'),')'), '')), IF(clients.lastname != '' OR clients.firstname != '', CONCAT(if(clients.lastname != '', clients.lastname, '{blank}'),', ',if(clients.firstname != '', clients.firstname, '{blank}')), ''))) AS thecount
            FROM
                ((`clients` INNER JOIN `addresstorecord` ON `clients`.`uuid` = `addresstorecord`.`recordid` AND `addresstorecord`.`tabledefid`='tbld:6d290174-8b73-e199-fe6c-bcf3d4b61083' AND addresstorecord.primary='1') INNER JOIN `addresses` ON  `addresstorecord`.`addressid` = `addresses`.`uuid`)
            WHERE
                (clients.inactive=0)

                AND ( (clients.company LIKE 'g%'
OR clients.firstname LIKE 'g%'
OR clients.lastname LIKE 'g%'
) OR ((clients.company LIKE 'g%' OR clients.company LIKE '% g%'
OR clients.firstname LIKE 'g%' OR clients.firstname LIKE '% g%'
OR clients.lastname LIKE 'g%' OR clients.lastname LIKE '% g%'
)))
           SELECT DISTINCT
                IF(clients.company != '', CONCAT(clients.company,IF(clients.lastname != '' OR clients.firstname != '', CONCAT(' (',if(clients.lastname != '', clients.lastname, '{blank}'),', ',if(clients.firstname != '', clients.firstname, '{blank}'),')'), '')), IF(clients.lastname != '' OR clients.firstname != '', CONCAT(if(clients.lastname != '', clients.lastname, '{blank}'),', ',if(clients.firstname != '', clients.firstname, '{blank}')), '')) AS display,
                `clients`.`uuid` AS value,
                IF(addresses.city != '' OR addresses.state !='' OR addresses.postalcode != '', CONCAT(IF(addresses.city != '',addresses.city,''),', ',IF(addresses.state != '', addresses.state, ''),' ',IF(addresses.postalcode != '', addresses.postalcode, '')),'unspecified location') AS secondary,
                clients.type AS classname
            FROM
                ((`clients` INNER JOIN `addresstorecord` ON `clients`.`uuid` = `addresstorecord`.`recordid` AND `addresstorecord`.`tabledefid`='tbld:6d290174-8b73-e199-fe6c-bcf3d4b61083' AND addresstorecord.primary='1') INNER JOIN `addresses` ON  `addresstorecord`.`addressid` = `addresses`.`uuid`)
            WHERE
                (clients.inactive=0)

                AND ( (clients.company LIKE 'g%'
OR clients.firstname LIKE 'g%'
OR clients.lastname LIKE 'g%'
) OR ((clients.company LIKE 'g%' OR clients.company LIKE '% g%'
OR clients.firstname LIKE 'g%' OR clients.firstname LIKE '% g%'
OR clients.lastname LIKE 'g%' OR clients.lastname LIKE '% g%'
)))
            ORDER BY
                IF(clients.company != '', CONCAT(clients.company,IF(clients.lastname != '' OR clients.firstname != '', CONCAT(' (',if(clients.lastname != '', clients.lastname, '{blank}'),', ',if(clients.firstname != '', clients.firstname, '{blank}'),')'), '')), IF(clients.lastname != '' OR clients.firstname != '', CONCAT(if(clients.lastname != '', clients.lastname, '{blank}'),', ',if(clients.firstname != '', clients.firstname, '{blank}')), ''))
            LIMIT 0, 8

我的输入

fromclause。

guests

displayfield:

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}')), ''))

secondaryfield:

classfield:

guests.id

searchfields:

guests.firstname, guests.lastname, guests.passport

filterclause:

guests.id=0

我的查询失败:

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 'AS secondary,
                guests.id AS classname
            FROM
                guests
            WHERE
                (guests' at line 4

Statement: 
            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

2 个答案:

答案 0 :(得分:0)

您没有使用辅助别名选择字段名称,应该类似

myField as secondary

用表格中的字段替换myField

答案 1 :(得分:0)

在失败查询的参数中,“secondaryfield”为空。这就是在“AS secondary”之前被替换的内容,因此结果不是有效的SQL。