Toon posts:

[php/mysql] probs met 3-table join

Pagina: 1
Acties:

Verwijderd

Topicstarter
ik heb drie tabellen: article, author, category. In article staan oa. de velden aid, auid en cid waarbij auid en cid verwijzen naar de tabellen author en category.

nu zou ik graag met 1 query basic gegevens van alle articles selecteren. Die komen voornamelijk uit de article tabel zelf, maar de author en de category komen uit die andere tabellen.

Nu zou ik gewoon de id's uit article kunnen lezen, en vervolgens die apart queryen op die tabellen. Maar het moet toch ook kunnen met 1 query.

Wat ik nu heb:
PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?
    //get basic article info
    $articlesQ = "SELECT a.aid,
                         a.aName,
                         a.aDate,
                         a.visible,
                         au.auName,
                         c.cName
                 FROM article a, author au, category c
                 GROUP BY aid";
    $articlesR = mysql_query($articlesQ);

    //process each article
    for ($i=0;$i<mysql_num_rows($articlesR);$i++) {
        $article = mysql_fetch_array($articlesR);

        $a = $article['aid'];
        $aName = $article['aName'];
        $auName = $article['auName'];
        $cName = $article['cName'];
        $visible = $article['visible'];
        $aDate = $article['aDate'];

        //echo table row
        echo "<tr>".
             "<td>$a</td>".
             "<td>$auName</td>".
             "<td>$cName</td>".
             "<td>$aName</td>".
             "<td>".date("j-d-Y",$aDate)."</td>".
             "<td>$visible</td>".
             "</tr>";
    }
?>

Nu krijg ik als author en als category "Unknown". Dat zijn de records met als auid en als cid 1. Maar dat klopt niet...

Wat doe ik fout?

  • Orphix
  • Registratie: Februari 2000
  • Niet online
Hoe zien je tabellen eruit? Wat zijn de 'relaties' tussen je tabellen?
Je gebruikt in je query geen WHERE constructie, waar moet de join vandaan komen dan?

Verwijderd

Topicstarter
tabellen:
article
-------
* aid int primary
* auid int
* cid int
* aName text
* aDate int
* visible enum('N','Y')
en nog een paar irrelevante zaken

author
------
auid int (primary, verwijst naar de auid van article)
auName text
...

category
--------
cid int (primary, verwijst naar de cid van article)
cName text ...


ik wil een tabelletje maken met een korte weergave van alle articles. dus zonder die auName en cName zou ik doen:
code:
1
SELECT aid,aName,aDate,visible FROM article;

[edit]voor twee tabellen is het minder moeilijk, dan zou ik
code:
1
SELECT a.id,a.aName,...,au.auName FROM article a, author au WHERE a.aid = au.aid;

maar die extra derde tabel zit me dwars. Kan je select statements nesten ofzo?

  • MikeN
  • Registratie: April 2001
  • Laatst online: 13-09 17:41
Op woensdag 02 januari 2002 23:23 schreef Jppr het volgende:
....maar die extra derde tabel zit me dwars. Kan je select statements nesten ofzo?
Niet in MySQL.

Hint: je kan 'meerdere' WHEREs doen dmv AND. Dus WHERE a=b AND b=c

  • Goodielover
  • Registratie: November 2001
  • Laatst online: 18-08 11:34

Goodielover

Only The Best is Good Enough.

Zo dus:
code:
1
2
3
4
5
6
7
8
SELECT a.*
    ,au.auName
    ,c.cName
FROM   article a
    ,author au
    ,cataegory c
WHERE  a.aid = au.aid
and    c.cid=a.cid;

Verwijderd

Topicstarter
thx, die AND werkt inderdaad.
Pagina: 1