比较像mysql的字面字符

时间:2016-02-24 04:30:19

标签: mysql sql-like

我有这张桌子:

|--------------------------|------------------|
|      tab1                |     tab2         |
|--------------------------|------------------|
| Guerreo Reyna            |         1        |
|--------------------------|------------------|
| Marroquin Bocanegra      |         10       |
|--------------------------|------------------|
| Calderon Salguero        |         10       |
|--------------------------|------------------|
| De la Cruz Barrios       |    1,2,3,4       |
|--------------------------|------------------|
| Guillen Cornet           |        1,3       |
|--------------------------|------------------|
| Luna Mendiví             |         10       |
|--------------------------|------------------|
| Chang Ruiz               |         1        |
|--------------------------|------------------|

当我执行此查询时:

SELECT DISTINCT f.familias,
                f.apellidos,
                i.nombre
FROM familias AS f
INNER JOIN integrantes AS i ON f.familias = i.familia
WHERE tab2 LIKE '%1%'
  OR tab2 LIKE '%3%'
ORDER BY 1

我只需要归还那些拥有1和3而不是10的人

非常感谢

2 个答案:

答案 0 :(得分:0)

SELECT DISTINCT f.familias,
                f.apellidos,
                i.nombre
FROM familias AS f
INNER JOIN integrantes AS i ON f.familias = i.familia
WHERE find_in_set('1',`tab2`) and find_in_set('3',`tab2`)
ORDER BY 1

答案 1 :(得分:0)

WHERE concat(',', tab2, ',') LIKE '%,1,%'
  OR  concat(',', tab2, ',') LIKE '%,3,%'
ORDER BY 1