这是我的 SQL 查询
select
tblUnderKategori.fldKategori,
tblUnderKategori.fldNavn,
tblUnderKategori.fldBillede,
tblKategori.fldId,
tblKategori.fldKategoriNavn
from
tblUnderKategori
inner join
tblKategori on tblUnderKategori.fldKategori=2
正如你所看到的,我需要 fldKategori = 2
处的所有内容,所以它确实如此,但它会写出 x2 次。
这是我需要显示的地方的后端代码
katFac objKat = new katFac();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack && !string.IsNullOrEmpty(Request.QueryString["id"]))
{
foreach (DataRow item in objKat.GetUnderkatByKat(Convert.ToInt32(Request.QueryString["id"])).Rows)
{
litUnderkategori.Text += item["fldNavn"].ToString() + "<br /><br />";
}
}
}
我似乎无法找出问题所在,所以请任何人帮忙
提前谢谢您! :)
最佳答案
您正在此处创建卡特斯乘积,因为您的联接缺少实际将两个表联接在一起的条件。
尝试使用这个:
select
tblUnderKategori.fldKategori,
tblUnderKategori.fldNavn,
tblUnderKategori.fldBillede,
tblKategori.fldId,
tblKategori.fldKategoriNavn
from
tblUnderKategori
inner join tblKategori
on tblUnderKategori.fldKategori = tblKategori.fldId
where tblUnderKategori.fldKategori=2
这假设tblUnderKategori.fldKategori
包含父类别的ID。
关于c# - SQL Server : INNER JOIN writes out things two times,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12350359/