webForumDet fria alternativet

join två tabeller med relationstabell

Databaser & SQL

5 svar · 353 visningar · startad av tlee

Medlem sedan mars 2001290 inlägg
Frågan#1

okej..

tblUser: (tabell med info om användaren)
fldUserId
fldUseName

tblCat: (tabell med de olika kategorierna)
fldCatId
fldCatName
fldCatActive = on/off

tblUserCat: ("relationstabell" som innehåller vilka kateogirer som användaren har valt som aktiv)
fldUserId
fldCatId
fldActive = on/off

hur ser en sql ut som plockar en lista på vilka kategorier som är vald som aktiv av användaren?

Medlem sedan dec. 200012 464 inlägg
#2
select fldUserName,fldCatName
from tblUser u, tblCat c, tblUserCat uc
where u.fldUserId = uc.fldUserId
and uc.active = 'Yes'
and uc.fldCatId = c.fldCatId
Medlem sedan mars 2001290 inlägg
#3

Microsoft OLE DB Provider for ODBC Drivers error '80004005'

[Microsoft][ODBC Microsoft Access Driver] The specified field 'fldUserId' could refer to more than one table listed in the FROM clause of your SQL statement.

select fldCompany, fldUserId
from tblUser u, tblCat c, tblUserCat uc
where u.fldUserId = uc.fldUserId 
and uc.fldActive = 'on' 
and uc.fldCatId = c.fldCatId;

hmm?

Medlem sedan juni 200032 967 inlägg
#4

I SELECT-delen, specificera vilken tabell du vill hämta fältet ifrån, precis som du gjort i resten av frågan.

Medlem sedan mars 2001290 inlägg
#5

ok.. nu får jag ett annat fel.. :)

select tblUser.fldCompany, tblUser.fldUserId from tblUser u, tblCat c, tblUserCat uc where u.fldUserId = uc.fldUserId and uc.fldActive = 'on' and uc.fldCatId = c.fldCatId;

Microsoft OLE DB Provider for ODBC Drivers error '80040e10'

[Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 2.

/t/default.asp, line 79

Medlem sedan dec. 20025 483 inlägg
#6
select u.fldCompany, u.fldUserId
from tblUser u, tblCat c, tblUserCat uc
where u.fldUserId = uc.fldUserId 
and uc.fldActive = on 
and uc.fldCatId = c.fldCatId;
256 ms totalt · 4 externa anrop · v20260731065814-full.1dc6f849
123 ms — deklarationer (db)
0 ms — hämta statistik (cache)
125 ms — hämta tråd, inlägg och bilagor (db)
128 ms — ändringar (db)