尝试:
SELECT distinct title
FROM
tag_news t1 inner join
tag_news t2 on
t1.idnews = t2.idnews inner join
tag_news t3 on
t2.idtag = t3.idtag inner join
news n on
n.idnews = t3.idnews
WHERE t1.idNews='$idNews'
抱歉,第一次和第二次错过了:
CREATE TABLE [dbo].news(
idnews [int] NOT NULL,
title [varchar](50) NULL
) ON [PRIMARY]
insert into news(idnews, title) values(1,'title 1')
insert into news(idnews, title) values(2,'title 2')
insert into news(idnews, title) values(3,'title 3')
insert into news(idnews, title) values(4,'title 4')
CREATE TABLE [dbo].tag_news(
idtag [int] NOT NULL,
idnews [int] NOT NULL,
) ON [PRIMARY]
insert into tag_news(idtag, idnews) values(1,1)
insert into tag_news(idtag, idnews) values(2,1)
insert into tag_news(idtag, idnews) values(1,2)
insert into tag_news(idtag, idnews) values(2,2)
insert into tag_news(idtag, idnews) values(2,3)
insert into tag_news(idtag, idnews) values(3,3)
insert into tag_news(idtag, idnews) values(4,3)
insert into tag_news(idtag, idnews) values(4,4)
select * from news;
select * from tag_news;
SELECT title FROM news WHERE idNews IN
(SELECT DISTINCT idNews FROM tag_news
WHERE idTag IN
(SELECT idTag FROM tag_news WHERE idNews=1));
SELECT distinct title
FROM
tag_news t1 inner join
tag_news t2 on
t1.idnews = t2.idnews inner join
tag_news t3 on
t2.idtag = t3.idtag inner join
news n on
n.idnews = t3.idnews
WHERE t1.idNews=1;