1. #1
    GMotha's Avatar
    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.

    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
    Zo toont deze dus alle albums (categoriën) en alle foto's daarvan. Dus een record per foto.
    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  

  2. #2
    Cycloon's Avatar
    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 character
    no votes  

  3. #3
    th43ns's Avatar
    Registered
    20/03/04
    Location
    Limburg
    Posts
    1,515
    iTrader
    12 (100%)
    Mentioned
    0 Post(s)
    Reputation
    0/4
    Quote Originally Posted by GMotha View Post
    This quote is hidden because you are ignoring this member. Show
    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.

    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
    Zo toont deze dus alle albums (categoriën) en alle foto's daarvan. Dus een record per foto.
    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.
    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 table
    no votes  

  4. #4
    Cycloon's Avatar
    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 character
    no votes  

  5. #5
    GMotha's Avatar
    Registered
    19/07/02
    Location
    ii
    Posts
    8,472
    iTrader
    8 (100%)
    Mentioned
    0 Post(s)
    Reputation
    0/99
    Quote Originally Posted by thaens View Post
    This quote is hidden because you are ignoring this member. Show
    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
    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  

  6. #6
    apa's Avatar
    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:
    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.DateEnd
    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.
    PC: 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 CP800
    no votes  

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Log in

Log in