like operator issue

后端 未结 3 1514
迷失自我
迷失自我 2021-01-27 14:06

I have a dom table

select * from dom

dom table details:

id    name     skills
1     do         


        
3条回答
  •  误落风尘
    2021-01-27 14:28

    in order to handle all possible cases (beginning of string, end of string, between 2 delimiters) you would need the inefficient (can't use an index)

    SELECT *
    FROM   dom
    WHERE  CONCAT(',', skills, ',') LIKE '%,C,%'
           AND CONCAT(',', skills, ',') LIKE '%,C++,%' 
    

    The best answer would be to put your database into at least first Normal Form!

    A separate skills table and skills/person matrix table is much more efficient to search and easier to work with (e.g. removing an individual skill)

    If you need a query that would bring back records in doms who match both C and C++ (assuming the same schema as jimmy_keen's answer) this could be done with a self join to get the intersection, the intersect operator, or using other relational division techniques such as

    SELECT d.name
    FROM   dom d
           JOIN dom_skills ds
             ON ( d.id = ds.id_dom )
           JOIN skills s
             ON ( ds.id_skill = s.id )
    WHERE  s.name IN ( 'C', 'C++' )
    GROUP  BY d.id,
              s.id,
              d.name
    HAVING COUNT(DISTINCT s.name) = 2 
    

提交回复
热议问题