php - 如何从 PHP 中的连接表中获取列?

标签 php mysql join

对于单个表,我通常会这样做:

$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 . '" 

关于php - 如何从 PHP 中的连接表中获取列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18006661/

相关文章:

php - Voicexml: <record> 提交截断音频消息

mysql - IFNULL() 等价于 PostgreSQL

mysql - 通过 IP (IPv4,IPv6) 获取国家位置

mysql - sql连接外键表

mysql - 成语中多列别名的示例

php - 教义不进行更新

php表单间歇性传递变量

javascript - 从 javascript 中的不同表单获取值并将其存储在 php session 中

mysql - 如何连接 2 个表 ide

mysql - 两个表的 SQL 对最新更新