douchuang1852 2014-05-15 21:52
浏览 147

MySQL搜索多个表

I'm in the midsts of constructing some database tables, but a possible search issue has just come to mind.

The two tables in question are Genres, a 2 column table holding a list of music genres identified by an ID field, i.e. 1 = Dance, 2 = Rock, and so on. And a Music table, a multi column table with Title, Artist, and Genre_ID fields. And yes you've guest it, Genre_ID refers to the ID of the Genre table.

My question is, if I have a search box on the site powered by PHP, and that search box queries the key fields, so Title, Artist, and Genre to yeld the best result, how can I get that to function correctly in a search, when the Genre name itself is in a separate table, and not in the Music table.

An example search would be, "rock music by ACDC".

  • 写回答

3条回答 默认 最新

  • dongtangu8615 2014-05-15 21:57
    关注

    Create a view where you join both of the tables. Then use SELECT with LIKE in WHERE clause or better use a fulltext search to do the searching job.

    The view

    create view ViewMusicWithGenre as
    select "*"
    from Music as m
    left join Genre as g on m.genre_id = g.id;
    

    Search option with like

    select "*"
    from ViewMusicWithGenre
    where Title like '%<what_you_search>%'
      or Artist like '%<what_you_search>%'
      or Genre like '%<what_you_search>%';
    

    I wrote the asterisk in "" because I KNOW that you WILL NOT use an asterisk. Left join is there because you want the row even without specified genre (very likely).

    The fulltext search

    This usually depends on the database you use. This is for instance Microsoft SQL Server 2014:

    Fulltext search - http://technet.microsoft.com/en-us/library/ms142571.aspx

    Fulltext index - http://technet.microsoft.com/en-us/library/ms187317.aspx

    Querying fulltext search - http://technet.microsoft.com/en-us/library/ms142583.aspx

    EDIT: for MySQL database

    MySQL does not support fulltext indeces on views. So you are left with couple of choices:

    • use the LIKE statement - could be ineffective, also more work later on
    • create the fulltext index on Music table and omit the genre - not good enough
    • create a new table that resembles the join and fill it on say daily basis with a job (or something like that) a do the fulltext search on that table - best solution in long terms, but more work to begin with and includes data duplicity

    You also have to bear in mind that fulltext indeces only work on MyISAM storage engine.

    The create statement for the joint table

    create table fulltextSearchTable (
        Music_ID int not null primary key,
        Music_Title varchar(1024) not null,
        Music_Artist varchar(1024) not null,
        Genre_ID int not null,
        Genre_Title varchar(1024) not null,
        fulltext(Music_Title, Music_Artist, Genre_Title)
    ) engine=MyISAM;
    

    The select with fulltext search

    select "*"
    from fulltextSearchTable
    where match(Music_Title, Music_Artist, Genre_Title) against ('your_keyword');
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度