一对多表上的mysql搜索查询

发布于 2024-12-09 01:24:30 字数 1931 浏览 0 评论 0原文

我有两个这样的表(_video,_video_tag)。

我如何构建最佳搜索查询?

我想在这个字段中搜索(标题、描述、标签)

mysql> describe _video;
+-------------+---------------+------+-----+-------------------+----------------+
| Field       | Type          | Null | Key | Default           | Extra          |
+-------------+---------------+------+-----+-------------------+----------------+
| id          | int(11)       | NO   | PRI | NULL              | auto_increment |
| user_id     | int(11)       | NO   | MUL | NULL              |                |
| image       | varchar(255)  | NO   |     | NULL              |                |
| source      | varchar(255)  | YES  |     | NULL              |                |
| duration    | int(11)       | NO   |     | NULL              |                |
| title       | varchar(255)  | NO   |     | NULL              |                |
| slug        | varchar(255)  | NO   |     | NULL              |                |
| description | text          | NO   |     | NULL              |                |
| order       | int(11)       | NO   |     | 0                 |                |
| hit         | int(11)       | NO   |     | 1                 |                |
| status      | enum('0','1') | NO   |     | 1                 |                |
| date_add    | timestamp     | NO   |     | CURRENT_TIMESTAMP |                |
+-------------+---------------+------+-----+-------------------+----------------+

mysql> select * from _video_tag limit 5;
+----------+------------------------------------+
| video_id | tag                                |
+----------+------------------------------------+
|    17748 | cevahir almanca dersi              |
|    17748 | genis aile                         |
|    17748 | ulvi                               |
|    17748 | cevahir                            |
|    17749 | oyle bir geçer zamanki
+----------+------------------------------------+

i have two table (_video, _video_tag) like this.

i how to building a best search query ?

i want search in this fields (title, description, tag)

mysql> describe _video;
+-------------+---------------+------+-----+-------------------+----------------+
| Field       | Type          | Null | Key | Default           | Extra          |
+-------------+---------------+------+-----+-------------------+----------------+
| id          | int(11)       | NO   | PRI | NULL              | auto_increment |
| user_id     | int(11)       | NO   | MUL | NULL              |                |
| image       | varchar(255)  | NO   |     | NULL              |                |
| source      | varchar(255)  | YES  |     | NULL              |                |
| duration    | int(11)       | NO   |     | NULL              |                |
| title       | varchar(255)  | NO   |     | NULL              |                |
| slug        | varchar(255)  | NO   |     | NULL              |                |
| description | text          | NO   |     | NULL              |                |
| order       | int(11)       | NO   |     | 0                 |                |
| hit         | int(11)       | NO   |     | 1                 |                |
| status      | enum('0','1') | NO   |     | 1                 |                |
| date_add    | timestamp     | NO   |     | CURRENT_TIMESTAMP |                |
+-------------+---------------+------+-----+-------------------+----------------+

mysql> select * from _video_tag limit 5;
+----------+------------------------------------+
| video_id | tag                                |
+----------+------------------------------------+
|    17748 | cevahir almanca dersi              |
|    17748 | genis aile                         |
|    17748 | ulvi                               |
|    17748 | cevahir                            |
|    17749 | oyle bir geçer zamanki
+----------+------------------------------------+

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暖风昔人 2024-12-16 01:24:30

如果您的表是 MyISAM 最好的可扩展方法是在 _video 表中的标题和描述上创建一个 FULLTEXT 索引:

CREATE FULLTEXT INDEX title_desc ON _video( title, description )

并在 _video_tag 表中的标签列上创建一个 FULLTEXT 索引,

CREATE FULLTEXT INDEX tag ON _video_tag( tag )

然后像这样查询:

( SELECT *,
  MATCH(title, description) AGAINT('some query string') AS relevance
  FROM _video
  WHERE MATCH(title, description) AGAINT('some query string') )
UNION
( SELECT v.*
  MATCH(vt.tag) AGAINST('some query string') AS relevance
  FROM _video_tag AS vt
  INNER JOIN _video AS v ON( v.id = vt.video_id )
  WHERE MATCH(vt.tag) AGAINST('some query string') )
ORDER BY relevance DESC

If your tables are MyISAM The best scalable way is to make a FULLTEXT index on title and description in _video table:

CREATE FULLTEXT INDEX title_desc ON _video( title, description )

and one on tag column in _video_tag table:

CREATE FULLTEXT INDEX tag ON _video_tag( tag )

and then query like this:

( SELECT *,
  MATCH(title, description) AGAINT('some query string') AS relevance
  FROM _video
  WHERE MATCH(title, description) AGAINT('some query string') )
UNION
( SELECT v.*
  MATCH(vt.tag) AGAINST('some query string') AS relevance
  FROM _video_tag AS vt
  INNER JOIN _video AS v ON( v.id = vt.video_id )
  WHERE MATCH(vt.tag) AGAINST('some query string') )
ORDER BY relevance DESC
四叶草在未来唯美盛开 2024-12-16 01:24:30

这取决于您想要获得什么,提供哪些信息。不过,我假设您希望从 _video_tag 中给定特定标签的 _video 中获取信息。然后,您

select v.* from _video_tag as vt left join _video as v on v.id = vt.video_id where vt.tag = 'Specify tag';

It depends on what you want to get, providing which information. However, I assume you want to get information from _video given a specific tag in _video_tag. Then you do

select v.* from _video_tag as vt left join _video as v on v.id = vt.video_id where vt.tag = 'Specify tag';

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文