简体   繁体   English

MySQL-具有RLIKE和GROUP BY的UNION SELECT-无法插入INNER JOIN

[英]MySQL - UNION SELECT with RLIKE and GROUP BY - not able to insert INNER JOIN

I created a mySQL query to search words and order results by relevancy (Sortkey). 我创建了一个mySQL查询,以按相关性搜索单词和结果(Sortkey)。 For example, if I search "test word", my query will look like this (and it works like a charm): 例如,如果我搜索“测试词”,我的查询将看起来像这样(它像一个超级按钮一样起作用):

SELECT * 
FROM (  SELECT *, 1 as SortKey 
        FROM equipments 
        WHERE (
            DESCRIPTION RLIKE '[[:<:]]test word[[:>:]]' 
            OR EQUIPMENT_ID RLIKE '[[:<:]]test word[[:>:]]' 
            OR CATEGORY RLIKE '[[:<:]]test word[[:>:]]'
        )

        UNION SELECT *, 2 as SortKey 
        FROM equipments 
        WHERE (
                (DESCRIPTION RLIKE '[[:<:]]test[[:>:]]' OR 
                EQUIPMENT_ID RLIKE '[[:<:]]test[[:>:]]' OR 
                CATEGORY RLIKE '[[:<:]]test[[:>:]]' OR 
                DESCRIPTION RLIKE '[[:<:]]tests[[:>:]]' OR 
                EQUIPMENT_ID RLIKE '[[:<:]]tests[[:>:]]' OR 
                CATEGORY RLIKE '[[:<:]]tests[[:>:]]') 

                AND

                (DESCRIPTION RLIKE '[[:<:]]word[[:>:]]' OR 
                EQUIPMENT_ID RLIKE '[[:<:]]word[[:>:]]' OR 
                CATEGORY RLIKE '[[:<:]]word[[:>:]]' OR 
                DESCRIPTION RLIKE '[[:<:]]words[[:>:]]' OR 
                EQUIPMENT_ID RLIKE '[[:<:]]words[[:>:]]' OR 
                CATEGORY RLIKE '[[:<:]]words[[:>:]]')
            )

        UNION SELECT *, 3 as SortKey 
        FROM equipments 
        WHERE (
                (DESCRIPTION RLIKE '[[:<:]]test[[:>:]]' OR 
                EQUIPMENT_ID RLIKE '[[:<:]]test[[:>:]]' OR 
                CATEGORY RLIKE '[[:<:]]test[[:>:]]' OR 
                DESCRIPTION RLIKE '[[:<:]]tests[[:>:]]' OR 
                EQUIPMENT_ID RLIKE '[[:<:]]tests[[:>:]]' OR 
                CATEGORY RLIKE '[[:<:]]tests[[:>:]]')
                ) 
                OR (
                (DESCRIPTION RLIKE '[[:<:]]word[[:>:]]' OR 
                EQUIPMENT_ID RLIKE '[[:<:]]word[[:>:]]' OR 
                CATEGORY RLIKE '[[:<:]]word[[:>:]]' OR 
                DESCRIPTION RLIKE '[[:<:]]words[[:>:]]' OR 
                EQUIPMENT_ID RLIKE '[[:<:]]words[[:>:]]' OR 
                CATEGORY RLIKE '[[:<:]]words[[:>:]]')
            )

) T1 GROUP BY EQUIPMENT_ID ORDER BY `SortKey` ASC

So, as I said, it works but I want to insert INNER JOIN in my query and I tried to put it everywhere and nothing works. 因此,正如我所说,它可以工作,但是我想在查询中插入INNER JOIN,然后尝试将其放置在任何地方,但没有任何效果。 Usually, to display all results without looking for keywords, I use this query (once again, it works fine): 通常,要显示所有结果而不寻找关键字,我使用此查询(再次,它工作正常):

SELECT
    `equipments`.`DESCRIPTION`,
    `equipments`.`EQUIPMENT_ID`,
    `equipments`.`CATEGORY`,
    `live_data`.`SERVICE`,
    `live_data`.`HOURS` 
FROM equipments 
INNER JOIN customers ON `customers`.`EQUIPMENT_ID` = `equipments`.`EQUIPMENT_ID`
LEFT JOIN live_data ON `live_data`.`EQUIPMENT_ID` = `equipments`.`EQUIPMENT_ID`
WHERE `customers`.`customer_id` = 1

I need to insert INNER JOIN to also add the WHERE statement to search into the customer's equipments only. 我需要插入INNER JOIN还要添加WHERE语句以仅搜索客户的设备。 Please help me! 请帮我!

select innerresult.* From
( Insert your query here ) innerresult
inner join customers On customers.equipment_id = innerresult.equipment_id
Where customers.customer_id = 1

Should do it I think. 我认为应该这样做。

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

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