在没有引用当前搜索的情况下在表中搜索的Mysql查询?

时间:2017-10-09 12:24:13

标签: mysql

我有一个HTML输入字段,在MySql表中搜索名称,并下拉显示找到的前15条记录的DIV。

名称显示为DISTINCT(所以唯一值)。

如果我在文字输入框中键入例如adr,则会显示“%adr%”的前15个匹配项。

我现在需要做的事情(而且我不能幸运),在同一文本中寻找2种不同类型的代码(位于codes_table_1codes_table_2)输入字段(以便扩展我现有的查询)。

2 codes_tables的结构将在下面(每个都有id和名称作为列):

code_id_1 | code_name_1
    1          XK112932
    2          XK082192
    3            ...

code_id_2 | code_name_2
    1          GG882931
    2          GG014253
    3            ...

所以我需要找到一种方法来添加到我的DISTINCT行,其中包含带有电话和地址的唯一人员,这些代码,就像在结果查询中添加新行一样。

例如,如果不是名字我寻找“KD139”,我需要获得开始/或在代码KD139中的第15个出现,将其查找到2个代码表中。

这是我对唯一名称的查询:

SELECT DISTINCT CONCAT(COALESCE(customer.last_name,''), ' ',
                             COALESCE(customer.first_name,'')) as fullName, 
                                    customer.first_name, 
                                    customer.last_name, 
                                    customer.email, 
                                    address.phone, 
                                    customer.customer_id

                            FROM customer

                            LEFT JOIN address
                            ON address.address_id = customer.address_id



                            WHERE CONCAT(COALESCE(customer.first_name,''), ' ', COALESCE(customer.last_name,'')) LIKE :search OR 
                                  CONCAT(COALESCE(customer.last_name,''), ' ', COALESCE(customer.first_name,'')) LIKE :search OR 
                                    customer.business_name LIKE :search OR 
                                    customer.email LIKE :search OR 
                                    address.phone LIKE :search

                            LIMIT 15

UPDATE(有效的解决方案!):

(SELECT DISTINCT 
    customer.customer_id,
    CONCAT(COALESCE(customer.last_name,''), ' ', COALESCE(customer.first_name,'')) as fullName,
    customer.email, 
    address.phone

FROM customer

LEFT JOIN address
ON address.address_id = customer.address_id

INNER JOIN th_table

WHERE CONCAT(COALESCE(customer.first_name,''), ' ', COALESCE(customer.last_name,'')) LIKE :search OR 
      CONCAT(COALESCE(customer.last_name,''), ' ', COALESCE(customer.first_name,'')) LIKE :search OR 
      customer.business_name LIKE :search OR 
      customer.email LIKE :search OR 
      address.phone LIKE :search 

ORDER BY fullName ASC

LIMIT 15)

UNION ALL

(SELECT code1_table.code1_id, 
    code1_table.code1_number, 
    'TH_TABLE' as email, 
    Null as phone

FROM code1_table

WHERE code1_table.code1_number LIKE :search

ORDER BY code1_table.code1_number ASC

LIMIT 15)

UNION ALL

(SELECT code2_table.code2_id, 
    code2_table.code2_number, 
    'CODE2_TABLE' as email, 
    Null as phone

FROM code2_table

WHERE code2_table.code2_number LIKE :search

ORDER BY code2_table.code2_number ASC

LIMIT 15)

1 个答案:

答案 0 :(得分:1)

假设您想要将artistsong表格一起搜索到搜索字词。你可以试试这样的事情。

  SELECT * 
    FROM (
            SELECT 'artist' as type, 
                   first as first, last as last, label as tag
              FROM artist 
             WHERE first LIKE '%searchterm%'
                OR last LIKE '%searchterm%'
                OR label LIKE '%searchterm%'
           UNION ALL
            SELECT 'song' as type, 
                   title as first, '' as last, '' as tag
              FROM song
             WHERE title LIKE '%searchterm%'
       )

这会为您提供一个结果集,其中包含来自其中两个表的信息,格式为UNION ALL是有意义的。然后,您可以根据应用过滤和排序结果。

我只对此示例代码提供一项保证: 会很慢。 LIKE '%thing'是一个臭名昭着的表演反模式。