简体   繁体   English

棘手的SQL进行智能搜索

[英]Tricky SQL for a smart search

I am using a form through a PHP CMS system, that defines custom fields and such. 我正在通过PHP CMS系统使用一种表单,该表单定义了自定义字段等。

One of the custom fields allows for an input field which does a smart search. 自定义字段之一允许进行智能搜索的输入字段。 What this means is that when you start typing, it shows the records that match, quite similar to google suggest. 这意味着当您开始键入内容时,它会显示匹配的记录,与Google建议非常相似。

The smartsearch input field relies on a stored mysql search, and this is what I am having trouble with, because it just looks too complex. smartsearch输入字段依赖于存储的mysql搜索,这就是我遇到的麻烦,因为它看起来太复杂了。

I am going to paste the SQl from an existing search, and then explain what I am trying to do. 我将粘贴现有搜索中的SQl,然后解释我要做什么。

There are different sql queries in a few columns, as follows: 几列中有不同的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: 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

I am having trouble understanding how these queries actually work. 我无法理解这些查询的实际工作方式。 displayfield and secondaryfield in particular seem very redundant. displayfield和secondaryfield似乎非常多余。

i don´t think my needs are so different from how the current example smartsearch field works...just instead of clients i want guests, and instead of address, i just want it to match firstname, lastname or passport number. 我认为我的需求与当前示例smartsearch字段的工作方式没有什么不同...只是我想要的客人不是我想要的客人,而不是地址,我只是希望它匹配名字,姓氏或护照号码。

I wonder if I even need secondaryfield in that case? 我想知道在这种情况下我是否还需要secondaryfield?

In particular, the inner join in the fromclause confuses me, as I don´t think I need to do that as all the guest information is in one table... 特别是,fromclause中的内部联接使我感到困惑,因为我不需要这样做,因为所有来宾信息都在一个表中...

Any assistance here is much appreciated, thankyou. 非常感谢您在这里的任何帮助。

I reformatted your code samples with some indenting so it's easier to read. 我用一些缩进重新格式化了代码示例,以便于阅读。 They were very long and scrolling horizontally it's hard to tell what's going on. 它们很长,并且水平滚动,很难说出正在发生什么。

Something like this appears to do nothing: 这样的事情似乎无能为力:

IF(addresses.state != '', addresses.state, '')

I would guess it was written by someone who didn't understand how NULL works in SQL, or someone who is accustomed to Oracle, where NULL and '' are equivalent. 我猜想它是由不了解NULL在SQL中如何工作的人或习惯于Oracle的人编写的,其中NULL和''是等效的。

Displayfield and secondaryfield have different content. Displayfield和Secondaryfield具有不同的内容。 Displayfield shows the person's company and name, whereas secondaryfield shows the person's address, state, and postal code. Displayfield显示人员的公司和名称,secondaryfield显示人员的地址,州和邮政编码。 These are not redundant. 这些不是多余的。

The complexity comes from trying to handle blank or unspecified content. 复杂性来自于尝试处理空白或未指定的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM