我在问我的问题之前搜查过:
我有两张桌子。
表信息:
no | name
1 | david
2 | kang
表 info_kill :
no | staffno | skillno
------------------------
1 | 1 | 1
2 | 2 | 1
3 | 2 | 2
4 | 2 | 3
staffno
表中的 info_skill
和no
表中的info
是外键。
我想得到:
name
包含' ka' skillno
1,2,3。所以,这是我的sql,
SELECT a.NAME
FROM (SELECT no,
NAME
FROM info
WHERE NAME LIKE '%ka%') AS a
INNER JOIN info_skill AS b
ON a.no = b.staffno
INNER JOIN info_skill AS c
ON a.no = c.staffno
INNER JOIN info_skill AS d
ON a.no = d.staffno
WHERE b.skillno = '1'
AND c.skillno = '2'
AND d.skillno = '3'
我想知道的是这部分。
INNER JOIN info_skill AS b
ON a.no = b.staffno
INNER JOIN info_skill AS c
ON a.no = c.staffno
INNER JOIN info_skill AS d
ON a.no = d.staffno
WHERE b.skillno = '1'
AND c.skillno = '2'
AND d.skillno = '3'
还有其他办法吗?
答案 0 :(得分:2)
试试这个:
SELECT I.name
FROM info I
JOIN info_skill S
ON I.no = S.staffno
WHERE I.name LIKE'%ka%" AND
S.skillno=1 OR
S.skillno=2 OR
S.skillno=3
这意味着:
LIKE %ka%
如果这不是您所需要的,那么请通过解释您正在寻找的内容来更清楚地说明您的问题。
答案 1 :(得分:1)
select a.name
from info a
inner join info_skill as b on a.no = b.staffno
where b.skillno in (1,2,3)
and name like '%ka%'
group by a.name
having count(distinct b.skillno) = 3