[英]Mysql Query with two tables php
我想知道如何用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和其他值,我该怎么做?
通过在FROM子句中指定两个表并在where子句中建立关系,可以在不使用JOIN关键字的情况下执行简单的联接查询。
例如
SELECT columns
FROM table1, table2
WHERE table1.field = table2.field
您在问有关SQL连接的问题,这是将两个或多个表放在一起的SQL语句中以从多个表中返回数据的实践。 您将表加入一个公共列,例如author.authorid = book.authorid。 我建议在Google上查找JOINS,有很多不错的文章。
一篇很棒的文章: http : //www.sitepoint.com/understanding-sql-joins-mysql-database/
听起来您正在寻找联接 。 尝试如下操作:
SELECT * FROM tb_columnas JOIN tb_autores ON tb_columnas = col_Autor WHERE col_Status='activo' ORDER BY col_ID DESC
您需要为此了解联接。
在这里,您会发现很好的解释:
http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.