php - mysql PHP 中连接表的问题

标签 php mysql sql join

当我使用此查询时,我会收到数组中变量的通知错误 - 如果我使用 $author_slug = $row['slug'];而不是 $author_slug = $row['authors.slug'];它有效,但我怎么知道哪些数据来自哪个表?

$select_quote=mysql_query("SELECT authors.name, authors.id, authors.img, authors.slug, quotes.author_id, quotes.title, quotes.id, quotes.meta_keys, quotes.meta_description, quotes.slug, quotes.content
                           from quotes, authors
                           WHERE quotes.author_id = authors.id
                       ORDER BY RAND() LIMIT 1 ");

    while ($row=mysql_fetch_array($select_quote)) {

    $author_id = $row['authors.id'];
    $author_name = $row['authors.name'];
    $author_slug = $row['authors.slug'];

    echo" $author_slug";
    }

最佳答案

您可以为此字段使用别名:

SELECT authors.name
     , authors.id
     , authors.img
     , authors.slug AS 'authors.slug'
     , quotes.author_id
     , quotes.title
     , quotes.id
     , quotes.meta_keys
     , quotes.meta_description
     , quotes.slug AS 'quotes.slug'
     , quotes.content
 FROM quotes, authors
 WHERE quotes.author_id = authors.id
 ORDER BY RAND() LIMIT 1 

因此您可以在 php 中访问检索到的数据,如下所示:

 $author_slug = $row['authors.slug']
 $quotes_slug = $row['quotes.slug']

关于php - mysql PHP 中连接表的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23763795/

相关文章:

php - mysql查询根据日期检索记录

php - 尝试对YouTube标记的RSS供稿进行排序,以对其进行排序(观看次数)

php - mySQL INSERT with TRIGGER 的计时问题

mysql - 在 SQl 中加入名称

sql - 与 Oracle 11g 中元组中的 null 进行比较

sql - Access SQL > 选择案例帮助

sql - SQL:汇总表数据分组

php - 单击提交按钮 PHP 更新下表值

mysql - DATETIME 和 TIMESTAMP 之间的同步

php - 选择嵌套查询 CodeIgniter