一对多表上的mysql搜索查询
我有两个这样的表(_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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的表是 MyISAM 最好的可扩展方法是在 _video 表中的标题和描述上创建一个 FULLTEXT 索引:
并在 _video_tag 表中的标签列上创建一个 FULLTEXT 索引,
然后像这样查询:
If your tables are MyISAM The best scalable way is to make a FULLTEXT index on title and description in _video table:
and one on tag column in _video_tag table:
and then query like this:
这取决于您想要获得什么,提供哪些信息。不过,我假设您希望从 _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 doselect v.* from _video_tag as vt left join _video as v on v.id = vt.video_id where vt.tag = 'Specify tag';