Thread: Vraagje SQL statement
-
25-09-2009, 15:31 #1Member
- Registered
- 19/07/02
- Location
- ii
- Posts
- 8,472
- iTrader
- 8 (100%)
- Mentioned
- 0 Post(s)
- Reputation
- 0/99
Vraagje SQL statement
Ik ben voor mezelf aan een projectje begonnen. Het zou in feite een web-applicatie zijn waar men afbeeldingen kan toevoegen aan albums en er massa's informatie kan hangen.
Voor een overzichtscherm wou ik per album de basisgegevens en een willekeurige afbeelding uit dat album weergeven. Geen idee hoe ik dat in één statement plaats.
Zo toont deze dus alle albums (categoriën) en alle foto's daarvan. Dus een record per foto.Code:SELECT a.Title, a.Description, a.Location, a.DateStart, a.DateEnd, b.Image FROM tCAT a, tPIC b WHERE b.Category = a.Id AND b.Image IS NOT NULL
Ik zou deze willen omvormen naar één enkele string die per album slechts één willekeurige foto toont (als voorbeeld).
Iemand een idee hoe ik dit oplos?
Ik zou aan de tCAT tabel een veld kunnen toevoegen die reeds een thumb weergeeft, maar dat zou geen random zijn.Last edited by GMotha; 25-09-2009 at 15:37.
no votes
-
-
25-09-2009, 19:02 #2Approved 9liver
- Registered
- 18/01/04
- Location
- Melle
- Posts
- 10,535
- iTrader
- 56 (100%)
- Mentioned
- 0 Post(s)
- Reputation
- 27/102
Zeg mss eerst ff welke database je gebruikt? Bij een MYSQL database is het praktisch onmogelijk om dit te bereiken.
“In terms of how we evaluate schooling, everything is about working by yourself. If you work with someone else, it’s called cheating. Once you get out in the real world, everything you do involves working with other people.”
PSN: Cycloon - Final Fantasy XIV: A realm reborn characterno votes
-
27-09-2009, 12:48 #3Member
- Registered
- 20/03/04
- Location
- Limburg
- Posts
- 1,515
- iTrader
- 12 (100%)
- Mentioned
- 0 Post(s)
- Reputation
- 0/4
Afhankelijk van de db hang je er iets anders in maar dit is voor sql server:
SELECT a.Title, a.Description, a.Location, a.DateStart, a.DateEnd, b.Image FROM tCAT a, tPIC b
WHERE a.id = (SELECT TOP 1 category FROM tPIC ORDER BY NEWID())
AND b.Image IS NOT NULL
Andere random selects vindt ge hier: SQL to Select a random row from a database tableno votes
-
27-09-2009, 14:30 #4Approved 9liver
- Registered
- 18/01/04
- Location
- Melle
- Posts
- 10,535
- iTrader
- 56 (100%)
- Mentioned
- 0 Post(s)
- Reputation
- 27/102
Ah, ik zocht het veel te ver, blijkbaar is het redelijk easy in mysql
“In terms of how we evaluate schooling, everything is about working by yourself. If you work with someone else, it’s called cheating. Once you get out in the real world, everything you do involves working with other people.”
PSN: Cycloon - Final Fantasy XIV: A realm reborn characterno votes
-
28-09-2009, 15:15 #5Member
- Registered
- 19/07/02
- Location
- ii
- Posts
- 8,472
- iTrader
- 8 (100%)
- Mentioned
- 0 Post(s)
- Reputation
- 0/99
Deze zou slechts 1 record opleveren, het is de bedoeling dat elke record uit tCAT weergegeven wordt. Maar met slechts 1 willekeurige image uit tPIC (elke record in tCAT is gelinked aan meerdere pics) per record.
Toch al bedankt voor de hulp zover.
no votes
-
28-09-2009, 22:31 #6Member
- Registered
- 17/01/03
- Location
- Semmerzake
- Posts
- 5,166
- iTrader
- 3 (100%)
- Mentioned
- 3 Post(s)
- Reputation
- 0/31
Ik ga ervan uit dat Image (in jouw geval) een varchar is en geen Binary ofzo:
Dat lijkt een ietwat vreemde syntax, maar ik merk dat dit soort queries vaak performanter is dan veel van de alternatieven; zeker in vergelijking met sub-queries.Code:SELECT Title = tCAT.Title, Description = tCAT.Description, Location = tCAT.Location, DateStart = tCAT.DateStart, DateEnd = tCAT.DateEnd, Image = SUBSTR(MIN(RIGHT('0000' + CONVERT(varchar(04), 1000 * RAND())) + tPIC.Image), 5) FROM tCAT INNER JOIN tPIC ON tPIC.Category = tCAT.id WHERE tPIC.Image IS NOT NULL GROUP BY tCAT.Title, tCAT.Description, tCAT.Location, tCAT.DateStart, tCAT.DateEndPC: R5-2600X | X370-Pro | 2x8GB | Samsung 960 Pro 512GB | WD 4TB | GTX 660 | Eizo CX240 | Steinberg UR22MkII | 2x JBL LSR305
MCE: C2D E8400 | GA-X38-DQ6 | 4GB | GT240 | Samsung 850 EVO 256GB | Origen S16V | Panasonic P46VT20 | Marantz SR7500 | JBL SCS178
NAS: G1840T | H87I+ | 8GB | Samsung 850 PRO 256GB | 5x WD Red 4TB | OpenMediaVault
PHOTO: 1D MkIIN | EF 24-70 f/2,8 L USM | EF 70-200 f/4 L USM | EF 100-400 f/4,5-5,6 L USM IS | 430EX | Selphy CP800no votes

