使用 SELF JOIN 搜索

Search with SELF JOIN

请帮助找出正确的 SQL 搜索语句。我使用 SELF JOIN 并得到了正确的结果,但我无法 CONCAT_WS 以正确的方式工作。

数据:

id  base_id name                                type
2   0       Alien Isolation                     Base
5   0       Aliens vs. Predator                 Base
4   0       Aliens versus Predator Classic 2000 Base
6   0       Assassin's Creed                    Base
8   2       Corporate Lockdown                  DLC
9   2       Crew Expendable                     DLC
10  2       Trauma                              DLC
11  2       Last Survivor                       DLC
12  2       Safe Haven                          DLC
13  2       Lost Contact                        DLC
14  1       The Trigger                         DLC
22  0       Age of Wonders III                  Base
23  2       Alien Batman                        DLC

SELECT 所有基础游戏:

    SELECT games.id, games.name, games.type, COUNT(dlc.base_id) AS dlc_count
    FROM games
    LEFT JOIN games dlc
    ON games.id = dlc.base_id
    WHERE games.base_id = 0
    GROUP BY games.id
    ORDER BY games.name ASC
    LIMIT 0, 10

结果:

id  name                                type    dlc_count
22  Age of Wonders III                  Base    0
2   Alien Isolation                     Base    7
4   Aliens versus Predator Classic 2000 Base    0
5   Aliens vs. Predator                 Base    0
6   Assassin's Creed                    Base    0

搜索单词 "alien" 显示 dlc 的基本名称

    SELECT games.id, CONCAT_WS(' -> ', games.name, dlc.name) AS name, games.type, COUNT(dlc.base_id) AS dlc_count
    FROM games
    LEFT JOIN games dlc
    ON games.id = dlc.base_id
    WHERE MATCH(games.name) AGAINST('alien*' IN BOOLEAN MODE)
    GROUP BY games.id
    ORDER BY games.name ASC
    LIMIT 0, 10

我得到了这些结果:

id  name                                    type    dlc_count   
23  Alien Batman                            DLC     0
2   Alien Isolation -> Corporate Lockdown   Base    7
4   Aliens versus Predator Classic 2000     Base    0
5   Aliens vs. Predator                     Base    0

他们应该是:

id  name                                    type    dlc_count   
23  Alien Isolation  -> Alien Batman        DLC     0
2   Alien Isolation                         Base    7
4   Aliens versus Predator Classic 2000     Base    0
5   Aliens vs. Predator                     Base    0

试试这个 SQL

select 
    id, 
    concat_ws(
        ' -> ',
        ( select base.name from games as base where base.id = games.base_id ),
        name
    ) as concat_name, 
    games.type, 
    ( select count(dlc.base_id) from games as dlc where dlc.base_id = games.id )  as dlc_count

from games where 
    match(name) against('alien*' in boolean mode)

order by name asc

limit 0, 10

你的逻辑有点难以理解。您似乎需要最后一列中所有 "dlc" 的计数。但是,您只想要包含 "Alien" 的 dlc 名称。我认为这符合您的要求:

SELECT g.id,
       CONCAT_WS(' -> ',
                 MAX(CASE WHEN lower(dlc.name) LIKE '%alien%' THEN dlc.name END), g.name) AS name, g.type,    
       COUNT(dlc.base_id) AS dlc_count
FROM games g LEFT JOIN
     games dlc
     ON g.id = dlc.base_id
WHERE MATCH(g.name) AGAINST('alien*' IN BOOLEAN MODE)
GROUP BY g.id, g.name
ORDER BY g.name ASC
LIMIT 0, 10