0

I was wondering how to do a query with two tables in php?

I have this single query

?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'];
        ?>

But I'd like to compare the col_Autor with au_Nombre which is in another table (tb_autores) and get au_Photo and other values from it, how can I do that?

2
  • Google MySQL JOIN. Also, the mysql_ family of functions has been deprecated. That means they will be removed in a future version of php. You should replace them with either the pdo functions or the mysqli functions Commented Jul 9, 2013 at 3:20
  • sidenote: stop using deprecated mysql_* functions. use MySQLi or PDO instead. Commented Jul 9, 2013 at 3:20

4 Answers 4

3

You can do a simple join query without using the JOIN keyword by specifying the two tables in the FROM clause and establishing a relationship in the where clause.

For example

   SELECT columns
   FROM table1, table2
   WHERE table1.field = table2.field
Sign up to request clarification or add additional context in comments.

Comments

1

You are asking about SQL Joins, the practicing of putting two or more tables together in an SQL statement to return data from more than 1 table. You join the tables on a common column, such as author.authorid = book.authorid. I suggest looking up JOINS on google, there are many good articles.

A great article on it: http://www.sitepoint.com/understanding-sql-joins-mysql-database/

Comments

1

It sounds like you are looking for a join. Try something like the following:

SELECT * FROM tb_columnas JOIN tb_autores ON tb_columnas = col_Autor WHERE col_Status='activo' ORDER BY col_ID DESC

Comments

1

You need to understand joins for this.

Here you will find very good explanation of the same:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.