swg1cor14
swg1cor14

Reputation: 1722

mySQL INNER JOIN a Better Option?

Would doing an INNER JOIN or LEFT JOIN or RIGHT JOIN work better for this? Or is there a better way to do this?

$customSQL = "SELECT * FROM customFieldNames";
                    $customQRY = mysql_query($customSQL);
                    while ($customOBJ = mysql_fetch_object($customQRY)){
                        $cvSQL = "SELECT `VALUE` FROM customFieldValues WHERE FIELDID = '".$customOBJ->FIELDID."' AND MEMID = '".$memFetch['MEMID']."'";
                        $cvQRY = mysql_query($cvSQL);
                        $cvFetch = mysql_fetch_array($cvQRY);
                        echo '<tr><td>'.$customOBJ->FIELDNAME.'</td><td><input type="text" name="cv_'.$customOBJ->FIELDNAME.'" value="'.$cvFetch['VALUE'].'" /></td></tr>';
                    }

Upvotes: 0

Views: 93

Answers (2)

ruakh
ruakh

Reputation: 183211

Your current code is equivalent to performing an INNER JOIN, in that it won't print anything unless there's a match between the two tables. Specifically, it's equivalent to this INNER JOIN:

SELECT customFieldNames.FIELDNAME,
       customFieldValues.VALUE
  FROM customFieldNames
  JOIN customFieldValues
    ON customFieldNames.FIELDID = customFieldValues.FIELDID
 WHERE customFieldValues.MEMID = ...

Upvotes: 0

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

Yes, something of this kind

//this is just a sample to let you know how it can be

select c1.somefield, c2.somefield from customFieldNames c1
left join customFieldValues c2 on(c1.somefieldId = c2.somefildId)
where 1=1

Upvotes: 1

Related Questions