mysql - 使用 SELF JOIN 搜索

标签 mysql sql full-text-search 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 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

搜索单词“外星人”,显示 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

关于mysql - 使用 SELF JOIN 搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30454370/

相关文章:

mysql - 当 SQL 年龄 = 当前日期时更新

sql - MySQL 查询需要帮助以将跨多个表的数据与用作列名的数据连接起来

MySQL 显示第三个不同值的最大和最小天数

sql - 多个表的 View 可以用于全文搜索吗?

php - MySQL JOIN 和 SUBSELECT CONCAT

MySQL 连接/联合结果来自更多表

MySQL嵌套查询不会运行,在SQL Server上运行

mysql - 这个子查询是如何工作的?

text - 如何在文档中找到一组关键字,所有/部分关键字都在一定距离截止?

search - 在 Solr 索引时删除变音符号