我想知道如何用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与另一个表中的au_Nombre(tb_autores)进行比较,并从中获取au_Photo和其他值,我该怎么做?
答案 0 :(得分:3)
通过在FROM子句中指定两个表并在where子句中建立关系,可以在不使用JOIN关键字的情况下执行简单的连接查询。
例如
SELECT columns
FROM table1, table2
WHERE table1.field = table2.field
答案 1 :(得分:1)
您询问的是SQL连接,即在SQL语句中将两个或多个表放在一起以从多个表中返回数据的实践。您可以在公共列上加入表,例如author.authorid = book.authorid。我建议在google上查找JOINS,有很多好文章。
一篇很棒的文章:http://www.sitepoint.com/understanding-sql-joins-mysql-database/
答案 2 :(得分:1)
听起来你正在寻找join。尝试以下内容:
SELECT * FROM tb_columnas JOIN tb_autores ON tb_columnas = col_Autor WHERE col_Status='activo' ORDER BY col_ID DESC
答案 3 :(得分:1)
你需要了解联接。
在这里你可以找到相同的非常好的解释:
http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html