请检查所附表格。
我需要编写查询以获取同时具有tage_id
和32的文档。
像select * from doc_tags where tag_id = 32 or tag_id = 26
之类的东西将无法正常工作,因为我将获得两个文档都包含32、26和(32,26)。
我需要的文档的标签ID只有32 **,标签不是26。**
注意
我不能使用document_id
作为参考。
答案 0 :(得分:1)
select * from doc_tags dt1
where (
select count(distinct tag_id) from doc_tags dt2
where dt2.document_id = dt1.document_id and tag_id in (26, 32)
) = 2 and dt1.tag_id in (26, 32)
或
with data as (
select *,
count(distinct tag_id) over (partition by document_id) as matches
from doc_tags where tag_id in (26, 32)
)
select * from data where matches = 2
答案 1 :(得分:1)
我会计算文档在这两个标签之间具有的不同标签的数量:
SELECT document_id
FROM doc_tags
WHERE tag_id IN (26, 32)
GROUP BY document_id
HAVING COUNT(DISTINCT tag_id) = 2
答案 2 :(得分:1)
如果仅在两个文档之间,则可以执行此操作
Select *
from doc_tags a
join doc_tags b on a.document_id = b.document_id
where a.tag_id = 26 and b.tag_id = 32
答案 3 :(得分:0)
尝试这种类型的SQL QUUERY
select * from doc_tags where tag_id IN (32,26)
答案 4 :(得分:0)
DECLARE @RESULT TABLE(doc_name VARCHAR(255))
INSERT INTO @RESULT
SELECT document_id
from doc_tags
WHERE tag_id =26 AND document_id IN
(SELECT document_id
from #tmp
WHERE tag_id =32)
SELECT *
FROM doc_tags
WHERE document_id in (SELECT doc_name
FROM @RESULT)
答案 5 :(得分:0)
SELECT * FROM doc_tags WHERE tag_id = 32 and tag_id = 26
上面只会返回tag_id的32和26
答案 6 :(得分:0)
SELECT
d1.*
FROM
doc_tags AS d1
INNER JOIN
doc_tags AS d2
ON
d1.documentid = d2.documentid AND
(
d1.documentid = 26 AND d2.documentid = 32 OR
d1.documentid = 32 AND d2.documentid = 26
)
WHERE
d1.id <> d2.id
答案 7 :(得分:0)
您还可以使用min()/max()
函数:
SELECT document_id
FROM doc_tags dt
WHERE tag_id IN (26, 32)
GROUP BY document_id
HAVING MIN(tag_id) <> MAX(tag_id);
答案 8 :(得分:-1)
<?php
Route::get('your-uri', function () {
$user = \Auth::user();
dd($user);
})->middleware('auth');