mySQL INNER加入更好的选择?

时间:2011-11-28 00:55:57

标签: php mysql

进行INNER JOIN或LEFT JOIN或RIGHT JOIN会更好吗?或者有更好的方法吗?

$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>';
                    }

2 个答案:

答案 0 :(得分:1)

是的,这种东西

//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

答案 1 :(得分:0)

您当前的代码相当于执行INNER JOIN,因为除非两个表之间匹配,否则它不会打印任何内容。具体来说,它等同于INNER JOIN

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