对于单个表,我通常会执行以下操作:

$length = 42;
$result = mysql_query ('SELECT * FROM table WHERE length = "' . $length . '"', $dbconn);
$rowsfound = mysql_num_rows ($result);
if ($rowsfound == 1) {
    $row = mysql_fetch_array ($result);
    $tableid = $row ['table_id'];
    $tablecloth = $row ['tablecloth'];
    $height = $row ['height'];
    ...


但是如何从这样的联接表中获取行:

$chairid = 123;
$result = mysql_query ('SELECT * FROM table,chair WHERE chair.id = "' . $chairid . '" AND table.table_id = chair.table_id', $dbconn);
$rowsfound = mysql_num_rows ($result);
if ($rowsfound == 1) {
    $row = mysql_fetch_array ($result);
    $tableid = $row ['table.table_id'];
    $tablecloth = $row ['table.tablecloth'];
    $height = $row ['table.height'];
    ...


这不会在$ table.tablecloth中返回任何值。我究竟做错了什么?

最佳答案

用JOIN代替

    SELECT * FROM table
    INNER JOIN chair  ON table.table_id = chair.table_id
    WHERE chair.id = "' . $chairid . '"

10-08 01:39