请帮助找出正确的搜索SQL语句。我使用SELF JOIN并获得正确的结果,但我无法让CONCAT_WS
以正确的方式工作。
DATA:
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
搜索单词" 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
答案 0 :(得分:1)
试试这个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
答案 1 :(得分:0)
你的逻辑有点难以理解。你似乎想要在最后一栏中计算所有“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