Forum FAQForum FAQSearchSearch MemberlistMemberlist Forum ignore listForum ignore list RegisterRegister ProfileProfile Log in to check your private messagesLog in to check your private messages Log inLog in
mysql.. problematičan query

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    mi3dot.org Forum Index -> Server-side
View previous topic :: View next topic  
Author Message
tmihetec



Joined: 23 Jan 2006
Posts: 27

PostPosted: 09.01.2008 23:26    Post subject: mysql.. problematičan query Add user to your forum ignore list Reply with quote

Pozdrav,

malo sam zapeo, ako je tko raspoložen neka pomogne...

dakle imam jednu tablicu sa recimo artiklima. Druga tablica predstavlja međusobne veze tih artikala...

tab1 = artiklID, naziv
tab2 = artikl1ID, artikl2ID

dakle druga tablica sadrži parove Id-jeva iz prve koji predstavljaju neku međusobnu vezu.

meni treba query koji će za zadani artiklID izvući /prema drugoj tablici/ i sve ostale artikle koji su s tim traženim u vezi. Problem je u tome što tražim elegantan način... moguće je napraviti sa unionom 2 selecta ... prvi put da pretpostavim da se key artikl nalazi u prvoj koloni pa da traži njegove parove kao artikl2ID i drugi put obrnuto...

mene zanima da li je to moguće nekako odraditi u 1 selectu...

hvala svima..
Tomek
Back to top
View user's profile Send private message
Moebius



Joined: 26 Jan 2004
Posts: 245
Location: zagreb

PostPosted: 10.01.2008 00:01    Post subject: Add user to your forum ignore list Reply with quote

Mozda ovako:
Code:
select tab1.artiklID, tab1.naziv from tab1
where artiklID in (select artikl2ID from tab2 where artikl2ID = tab1.artiklID and artikl1ID = @zadani_id)


napamet pisano, gdje je @zadani_id jednak trazenom artiklu.
Back to top
View user's profile Send private message Visit poster's website
tmihetec



Joined: 23 Jan 2006
Posts: 27

PostPosted: 10.01.2008 00:37    Post subject: Add user to your forum ignore list Reply with quote

ne radi... vraća samo jedan rezultat...

ali hvala na pokušaju Wink
Back to top
View user's profile Send private message
fun.ky



Joined: 05 Jan 2006
Posts: 365
Location: Mostar

PostPosted: 10.01.2008 13:02    Post subject: Add user to your forum ignore list Reply with quote

Postavi strukturu tablica

_________________
There is no style definition for good music!
Back to top
View user's profile Send private message Visit poster's website Twitter profile
tmihetec



Joined: 23 Jan 2006
Posts: 27

PostPosted: 10.01.2008 21:36    Post subject: Add user to your forum ignore list Reply with quote

CREATE TABLE `tab1` (
`artID` int(6) unsigned NOT NULL auto_increment,
`name` varchar(Cool default NULL,
PRIMARY KEY (`artID`)
)

CREATE TABLE `tab2` (
`vezaID` int(6) unsigned NOT NULL auto_increment,
`art1ID` tinyint(4) default NULL,
`art2ID` tinyint(4) default NULL,
PRIMARY KEY (`vezaID`)
)

... recimo...

hvala!
Back to top
View user's profile Send private message
zytzagoo
mi3.crew


Joined: 25 Aug 2003
Posts: 1842
Location: Zagreb, Hrvatska

PostPosted: 11.01.2008 00:01    Post subject: Add user to your forum ignore list Reply with quote

http://www.tonymarston.net/php-mysql/many-to-many.html ?

Ili pokusavas napraviti nesto drugacije?

_________________
[+]I[+]am[+]my[+]own[+]religion[+]
Back to top
View user's profile Send private message Visit poster's website Twitter profile
fun.ky



Joined: 05 Jan 2006
Posts: 365
Location: Mostar

PostPosted: 11.01.2008 09:59    Post subject: Add user to your forum ignore list Reply with quote

tmihetec wrote:
CREATE TABLE `tab1` (
`artID` int(6) unsigned NOT NULL auto_increment,
`name` varchar(Cool default NULL,
PRIMARY KEY (`artID`)
)

CREATE TABLE `tab2` (
`vezaID` int(6) unsigned NOT NULL auto_increment,
`art1ID` tinyint(4) default NULL,
`art2ID` tinyint(4) default NULL,
PRIMARY KEY (`vezaID`)
)

... recimo...

hvala!


Ako sam dobro shvatio to je onda ovo

Code:
SELECT t2.* FROM tab1 t1
Inner Join tab2 t2 on (t2.Art1ID = t1.ArtID OR t2.Art2ID = t1.ArtID)
Where t1.ArtID = 1

_________________
There is no style definition for good music!
Back to top
View user's profile Send private message Visit poster's website Twitter profile
tmihetec



Joined: 23 Jan 2006
Posts: 27

PostPosted: 11.01.2008 20:18    Post subject: Add user to your forum ignore list Reply with quote

zapravo i nije.... ali skužio sam - zapravo je rješenje kombinacija i hinta iz zytzagooovog odgovora i odgovora fun.kya. Problem kod fun.kyevog rješenja je što ono ispisuje cijelu tablicu dakle i jednu i drugu kolonu, a ja sam htio dobiti samo jednu kolonu sa IDjevima artikala koji su povezani sa zadanim - s time da u drugoj tablici parovi nisu uređeni, odnosno može biti popunjena na bilo koji način...

tab1
====
id---naziv
1-----ABC
2-----DEF
3-----GHI

tab2
====
vezaid---artikl1ID---artikl2ID
---- 1 --------- 2 ------------ 1
---- 2 --------- 3 ------------ 2
---- 3 --------- 2 ------------ 2

I sada... ja ako odaberem artikl iz prve tablice sa ID=2, želim da mi query vrati (na osnovu druge tablice)

rezultat:
1
3
2


rješenje:
SELECT
CASE WHEN t2.art1ID=2 THEN t2.art2ID ELSE t2.art1ID END AS povezi_sa
FROM tab1 t1
Inner Join tab2 t2 on (t2.Art1ID = t1.ArtID OR t2.Art2ID = t1.ArtID)
Where t1.ArtID = 2
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    mi3dot.org Forum Index -> Server-side All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group