简体   繁体   中英

one to many DB output - multiple loops or queries?

I have two MySQL tables. Table "dbt1" has unique records with a primary key "UID".

Table "dbt2" uses that key, but has multiple records per UID.

Using PHP and the proper MySQL query, how do I get records out of dbt1, then look through dbt2 to find all records with the same UID, then print those in succession until done. Then, go back and print the next record from dbt1, until the where clause is satisfied?

This is what I have so far:

Query:

$dbname = 'ext'; 
$dbt1 = 'questions';
$dbt2 = 'stats';
$data = "questions.UID qUID, QUESTION, ANSWER_A, ANSWER_B, ANSWER_C, ANSWER_D, FIGURE, CCAstats.UID sUID, DATE, PROP, CRPBI";    

$mysql_link = mysql_connect($dbhost,$dbuser,$dbpass);
$sql = ("SELECT $data FROM $dbt1 LEFT JOIN $dbt2 ON $dbt1.UID = $dbt2.UID WHERE $dbt1.SECTION = $section ORDER BY $dbt1.UID;");
$results = mysql_db_query($dbname,$sql) or die("no results");

if ($results)
{
    while($row = mysql_fetch_assoc($results))
{
        print "$row[qUID]"." "."$row[QUESTION]"."<br>";
        print "A. $row[ANSWER_A]"."<br>";
        print "B. $row[ANSWER_B]"."<br>";
        print "C. $row[ANSWER_C]"."<br>";
        print "D. $row[ANSWER_D]"."<br>";
        print "$row[FIGURE]"."<br>";
        print "$row[DATE]".", "."$row[PROP]".", "."$row[CRPBI]"."<br>";
     }

     mysql_free_result($results);
 }
 mysql_close();

This works to output all results based on subject (passed as a POST variable), and duplicates output if there is more than one record in $dbt2 for any given record in $dbt1.

The issue is, I don't want duplicate output from $dbt1. I want one entry returned for all records in $dbt1, and as many records in $dbt2 as relate to the UID in $dbt1. Make sense?

So, the output would be something like:

UID QUESTION 1
A. ANSWER A
B. ANSWER B
C. ANSWER C
D. ANSWER D
DATE, PROP, CRPBI
DATE{2}, PROP{2}, CRPBI{2}

UID QUESTION 2
A. ANSWER A
B. ANSWER B
C. ANSWER C
D. ANSWER D
DATE, PROP, CRPBI
DATE{2}, PROP{2}, CRPBI{2}

My question is, do I need another loop somewhere to get all results from $dbt2, or is there something I can do in the query? Do I need a second query? I need to report all $dbt2 entries per $dbt1 record for any given UID. I feel like I can't even ask the question properly...sorry.

You don't have a query problem. You are querying properly (as far as I can tell). You've got a DISPLAY problem. Essentially you need to give you loop some memory, creating a simple state machine:

$prev_dbt1 = null;
while($row = mysql_fetch($result)) {
   if ($prev_dbt1 <> $row['dbt1']) {
       ... display new table header ...
       $prev_dbt1 = $row['dbt1']; // update for next loop
   }
   display other stuff as usual
}

Here's what I came up with. It works, but I'm not sure how efficient it is. Luckily, my tables are fairly small.

$query1 = "SELECT * FROM $dbt1 WHERE $dbt1.SECTION = $section ORDER BY $dbt1.UID";
$query1Result = mysql_db_query($dbname,$query1) or die("no query");

while ($row = mysql_fetch_assoc($query1Result)) 
{
    echo $row['UID']." ".$row['QUESTION']."\n";
    echo $row['ANSWER_A']."\n";
    echo $row['ANSWER_B']."\n";
    echo $row['ANSWER_C']."\n";
    echo $row['ANSWER_D']."\n";
    echo $row['FIGURE']."\n";
    $query2 = "SELECT * FROM $dbt2 WHERE $row[UID] = $dbt2.UID ORDER BY $dbt2.DATE";
    $query2Result = mysql_db_query($dbname,$query2) or die("no query2");
    while ($stat = mysql_fetch_assoc($query2Result))
    {
        echo $stat['DATE']."\n";
        echo $stat['PROP']."\n";
        echo $stat['CRPBI']."\n\n";
    }

}    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM