DROP TABLE IF EXISTS diaries, users;
SET NAMES utf8mb4;
CREATE TABLE users (
id INT PRIMARY KEY AUTO_INCREMENT,
name TEXT,
FULLTEXT INDEX (name)
) DEFAULT CHARSET utf8mb4;
CREATE TABLE diaries (
id INT PRIMARY KEY AUTO_INCREMENT,
user_id INT UNSIGNED NOT NULL,
title TEXT,
FULLTEXT INDEX (title)
) DEFAULT CHARSET utf8mb4;
INSERT INTO users (name) VALUES ('user 1');
INSERT INTO users (name) VALUES ('user 2');
INSERT INTO users (name) VALUES ('user 3');
SELECT * FROM users;
id	name
1	user 1
2	user 2
3	user 3
INSERT INTO diaries (user_id, title) VALUES (1, 'Hello!');
INSERT INTO diaries (user_id, title) VALUES (2, 'my name is bob');
INSERT INTO diaries (user_id, title) VALUES (3, 'my name is carlos');
SELECT * FROM diaries;
id	user_id	title
1	1	Hello!
2	2	my name is bob
3	3	my name is carlos
SELECT id,
name,
MATCH(name) AGAINST('+user' IN BOOLEAN MODE) as score
FROM users
WHERE MATCH(name) AGAINST('+user' IN BOOLEAN MODE) AND
id IN (SELECT user_id
FROM diaries
WHERE MATCH(title) AGAINST('+name' IN BOOLEAN MODE))
ORDER BY score DESC
LIMIT 2;
id	name	score
2	user 2	1
3	user 3	1
SHOW STATUS LIKE 'mroonga_fast_order_limit';
Variable_name	Value
Mroonga_fast_order_limit	0
DROP TABLE diaries, users;
