Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在mysql数据库中执行此搜索的最佳方式是什么?_Mysql_Sql - Fatal编程技术网

在mysql数据库中执行此搜索的最佳方式是什么?

在mysql数据库中执行此搜索的最佳方式是什么?,mysql,sql,Mysql,Sql,我有3个表及其各自的结构: users: id(1),name colors: id(2),name,tags hobbies: id(3),name,tags userSettings: id,int(1) userid,int(2) color,int(3) hobbie 表值: users: {1,john},{2,ana} colors: {1,blue,blue azul celeste },{2,red,red vermelho rosso} hobbies: {1,sing,

我有3个表及其各自的结构:

users: id(1),name
colors: id(2),name,tags
hobbies: id(3),name,tags
userSettings: id,int(1) userid,int(2) color,int(3) hobbie
表值:

users: {1,john},{2,ana}

colors: {1,blue,blue azul celeste },{2,red,red vermelho rosso}

hobbies: {1,sing,cantare cantar sing},{2,run,running correr runner}

userSettings: {1,1,1,2},{2,2,2,1}
所以。。。我需要的是找到哪些用户拥有用户搜索的颜色或hobbie。例如:如果我按blue或runner搜索,我必须找到用户1-John

如果我通过rosso或sing搜索,我必须找到用户2-Ana。我知道如何使用3个查询来实现这一点,但我知道有一种方法可以将这一点改进为只使用一个查询,因为字段已被索引


有人能帮上忙吗?

这真的取决于你是如何实现你的搜索的,我想你是在使用PHP吧

一般来说,如果用搜索表单中的变量替换单词,类似的操作可能会起作用:

模式MySQL v5.7

问题1


以下查询应按预期完成作业。逻辑是使用userSettings作为桥梁,将users表与colors和cabiods表左连接。在联接条件中选中搜索参数。然后,WHERE子句确保至少有一种爱好或颜色与搜索参数匹配:

SELECT u.*
FROM 
    users u
    LEFT JOIN userSettings us ON us.user_id = u.id
    LEFT JOIN colors c 
        ON c.id = us.color_id  
        AND ( c.name = @search OR c.tags LIKE CONCAT('%', @search, '%') )
    LEFT JOIN hobbies  h 
        ON h.id = us.hobbie_id 
        AND (h.name = @search OR h.tags LIKE CONCAT('%', @search, '%') )
WHERE
    c.id IS NOT NULL OR h.id IS NOT NULL
您可以在下面的示例数据中看到它的操作。例如,搜索“sing”时,返回:

| id  | name |
| --- | ---- |
| 2   | Ana  |

向要搜索的表添加全文索引,然后在查询中联接表

模式:

查询:

也可以创建一个视图表。如:

模式

质疑

试试这个:

SELECT DISTINCT u.id || ' - ' || u.name as USER
FROM userSettings us
JOIN users u
    ON u.id = us.userid
JOIN colors c
    ON c.id = us.color
JOIN hobbies h
    ON h.id = us.hobbie
WHERE 
    c.name LIKE '%blue%'
    OR c.tags LIKE '%blue%'
    OR h.name LIKE '%runner%'
    OR h.tags LIKE '%runner%';
输入: 搜索蓝色或蓝色跑步者

输出:


不清楚您的数据是JSON还是普通的MySQL表行?
| id  | name |
| --- | ---- |
| 2   | Ana  |
CREATE TABLE users (
id int NOT NULL AUTO_INCREMENT,
name varchar(255),
PRIMARY KEY (id)
);
INSERT INTO users (name) VALUES ('john'), ('ana');

CREATE TABLE colors (
id int NOT NULL AUTO_INCREMENT,
name varchar(255),
tags varchar(255),
PRIMARY KEY (id),
FULLTEXT (tags)
);
INSERT INTO colors (name, tags)
VALUES
('blue','blue azul celeste'), 
('red','red vermelho rosso');

CREATE TABLE hobbies (
id int NOT NULL AUTO_INCREMENT,
name varchar(255),
tags varchar(255),
PRIMARY KEY (id),
FULLTEXT (tags)
);
INSERT INTO hobbies (name, tags)
VALUES
('sing','cantare cantar sing'), 
('run','running correr runner');

CREATE TABLE userSettings (
id int NOT NULL AUTO_INCREMENT,
userid int,
color int,
hobbie int,
PRIMARY KEY (id)
);
INSERT INTO userSettings (userid, color, hobbie)
VALUES
(1,1,2), 
(2,2,1);
SELECT users.* FROM users 
LEFT JOIN userSettings ON userSettings.userid = users.id 
LEFT JOIN colors ON colors.id = userSettings.color 
LEFT JOIN hobbies ON hobbies.id = userSettings.hobbie 
WHERE 
MATCH(colors.tags) AGAINST ('runner' IN BOOLEAN MODE)
OR MATCH(hobbies.tags) AGAINST ('runner' IN BOOLEAN MODE)
;
//As above +

CREATE VIEW searchview AS (SELECT userid, users.name as name, concat_ws(' ',colors.tags,hobbies.tags) as tags FROM users 
LEFT JOIN userSettings ON userSettings.userid = users.id 
LEFT JOIN colors ON colors.id = userSettings.color 
LEFT JOIN hobbies ON hobbies.id = userSettings.hobbie);
SELECT * FROM searchview
WHERE tags REGEXP '[[:<:]]runner[[:>:]]';
SELECT DISTINCT u.id || ' - ' || u.name as USER
FROM userSettings us
JOIN users u
    ON u.id = us.userid
JOIN colors c
    ON c.id = us.color
JOIN hobbies h
    ON h.id = us.hobbie
WHERE 
    c.name LIKE '%blue%'
    OR c.tags LIKE '%blue%'
    OR h.name LIKE '%runner%'
    OR h.tags LIKE '%runner%';
USER
1 - John