dongliang2058 2014-08-14 18:10
浏览 145
已采纳

如果一列有多个关键字,如何从数据库中获取结果?

Well, I've a table called keywords and there are 2 columns.

1) kid
2) keywords (keyword value)

and I've another table called contact_details where there a column called keyword. In this keyword column I'm inserting many keyword from keywordstable. So 2 tables is look like this...

Keywords table:

kid    keyword
1      php     
1      mysql     
1      html     
1      css     
1      css3     
1      wp     
1      photoshop     
1      3d

contact_details table:

cid    name    phone   keyword
1      alex    123     php, mysql, hmtl
2      alex1   124     php, html, css3
3      alex2   125     wp, html, css
4      alex3   126     photoshop, 3d
5      alex4   127     html, 3d, php
6      alex5   128     mysql, wp, html

Now I've a search box which is searching people (name From contact_details table) by using keyword value. In search box, Search value could be few keywords. I mean it's could be php, mysql, html or could be php, 3d, photoshop.

So My question is : how can I write a Sql query to get the result ? I want to get all name which match the search keyword/s from contact_details table ?

Is there any field need to add in keywords table ? Can not get an IDEA :(

  • 写回答

3条回答 默认 最新

  • douba8758 2014-08-14 18:28
    关注
    Select name from contact_details where keyword like '%<search keywords>%'
    

    Like search keyword key php then you need pass php in query and will get list of all name which having keywords as php'

    Select name from contact_details where keyword like '%php%'
    

    hope this will resolve your issue.

    Proper approach, Make kid as primary key in Keyword table

    Keywords table:
    kid    keyword
    1      php     
    2      mysql     
    3      html 
    

    Remove keywords column from contact_details table.

    contact_details table:
    cid    name    phone   
    1      alex    123     
    2      alex1   124     
    3      alex2   125 
    

    Make one more table which having many to many relationship and you need insert the relationship here so that no need touch again keywords and contact_details table again.

    keyword_contact_mapping
    kcid   kid_fk   cid_fk
    1      1        1     
    2      1        2
    3      1        3
    4      2        1    
    5      2        1 
    6      2        2
    7      2        3
    

    Sql query (Not tested you can also use alias)

    select name from contact_details join keyword_contact_mapping on kid_fk =(select kid from Keywords where keyword='php')
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R