表:taxonomy_term_data
tid name
7 desert
10 indian
表:taxonomy_index
nid tid
13 7
10 7
10 10
12 7
14 10
我需要一个mysql查询来获取具有“沙漠”和“印度”的nid。 这里的输出是nid = 10。 有人可以帮助编写查询。我只是个初学者
答案 0 :(得分:2)
SELECT nid
FROM taxonomy_index
JOIN taxonomy_term_data USING (tid)
WHERE name IN ('desert', 'indian')
GROUP BY nid
HAVING COUNT(DISTINCT tid) = 2
查看在线工作:sqlfiddle
答案 1 :(得分:1)
您可以通过GROUP BY和HAVING获得此答案:
SELECT nid
FROM taxonomy_index
WHERE tid IN (7, 10)
GROUP BY nid
HAVING COUNT(*) > 1
答案 2 :(得分:1)
你可以用各种SQL写作风格来编写它,如下所示:
1
SELECT DISTINCT(i.nid)
FROM taxonomy_index i
INNER JOIN taxonomy_term_data d
ON i.tid = d.tid
AND (d.name = 'desert'
OR d.name = 'indian')
2
SELECT i.nid
FROM taxonomy_index i
INNER JOIN taxonomy_term_data d
ON i.tid = d.tid
AND (d.name = 'desert'
OR d.name = 'indian')
GROUP BY nid
3
SELECT i.nid
FROM taxonomy_index i, taxonomy_term_data d
WHERE i.tid = d.tid
AND d.name IN ('desert', 'indian')
GROUP BY nid
4
SELECT DISTINCT(nid)
FROM taxonomy_index
WHERE (tid = 7
OR tid = 12)
答案 3 :(得分:0)
假设(nid, tid)
组合是唯一的:
SELECT ta.nid
FROM taxonomy_index AS ta
JOIN taxonomy_index AS tb
ON tb.nid = ta.nid
WHERE ta.tid = 7
AND tb.tid = 10