0

我想知道如何在 php 中使用两个表进行查询?

我有这个单一的查询

?php 
$sQuery = "Select * From tb_columnas Where col_Status='activo' Order by col_ID DESC";
$result = mysql_query($sQuery, $cnxMySQL) or die(mysql_error());
$rows_result = mysql_fetch_assoc($result);
$total_rows_result = mysql_num_rows($result);

if ($total_rows_result > 0){
    do {
            $id_columnas = $rows_result ['col_ID'];
            $col_Titulo = $rows_result ['col_Titulo'];
            $col_Resumen = $rows_result ['col_Resumen'];
            $col_Fecha = $rows_result ['col_Fecha'];
            $col_Autor = $rows_result ['col_Autor'];
        ?>

但我想将 col_Autor 与另一个表(tb_autores)中的 au_Nombre 进行比较,并从中获取 au_Photo 和其他值,我该怎么做?

4

4 回答 4

3

通过在 FROM 子句中指定两个表并在 where 子句中建立关系,您可以在不使用 JOIN 关键字的情况下进行简单的连接查询。

例如

   SELECT columns
   FROM table1, table2
   WHERE table1.field = table2.field
于 2013-07-09T03:29:10.053 回答
1

您在询问 SQL 连接,即在 SQL 语句中将两个或多个表放在一起以从多个表返回数据的做法。您将表连接到一个公共列上,例如 author.authorid = book.authorid。我建议在google上查找JOINS,有很多好文章。

一篇很棒的文章:http ://www.sitepoint.com/understanding-sql-joins-mysql-database/

于 2013-07-09T03:20:55.373 回答
1

听起来您正在寻找加入。尝试以下操作:

SELECT * FROM tb_columnas JOIN tb_autores ON tb_columnas = col_Autor WHERE col_Status='activo' ORDER BY col_ID DESC
于 2013-07-09T03:22:10.707 回答
1

为此,您需要了解联接。

在这里你会找到很好的解释:

http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html

于 2013-07-09T03:23:06.963 回答