Php-mysql Course

In this tutorial you can learn how to select data from two MySQL tables with a single query.
When the SQL query selects columns from diferent tables, you must specify the table name and the column; the syntax is:
SELECT `table1`.`column`, `table2`.`column` FROM `table1`, `table2` WHERE conditon

Let's see some examples, in which we'll use the following two tables, named "categories" and "links".
categories
idcategory
1 PHP-MySQL
2 HTML
links
idlinkvisits
1 coursesweb.net/php-mysql/arrays 12
1 coursesweb.net/php-mysql/strings 15
2 coursesweb.net/html/html-tables 18

1. Select all the columns from "categories" table where id=2, and the "link" and "visits" columns where visits>13.
SELECT `categories`.*, `links`.`link`, `links`.`visits` FROM `categories`, `links` WHERE `categories`.`id`=2 AND `links`.`visits`>13
Result:
| id | category |                link             | visits |
-----------------------------------------------------------------
| 2 |   HTML    | coursesweb.net/php-mysql/strings |   15   |
| 2 |   HTML    | coursesweb.net/html/html-tables  |   18   |


2. Select the "category", and the "link" columns, where the 'id' in the 'categories' table is 1 and the id in the 'links' table is equal with the 'id' of the 'categories'.
SELECT `categories`.`category`, `links`.`link` FROM `categories`, `links` WHERE `categories`.`id`=1 AND `categories`.`id`=`links`.`id`
Result:
| category  |               link                   |
----------------------------------------------------
| PHP-MySQL | coursesweb.net/php-mysql/arrays  |
| PHP-MySQL | coursesweb.net/php-mysql/strings |

Two Selects in a Query

You can also perform two select commands in a SQL query.
Examples:

1. Select the "link" column (from 'links') where the "id" corresponds to the HTML value (stored in the 'categories').
SELECT `link` FROM `links` WHERE `id`=(SELECT `id` FROM `categories` WHERE `category`='HTML')
Result:
|               link                  |
---------------------------------------
| coursesweb.net/html/html-tables |


2. Counts all rows in the 'links' table, where visits>14, and selects "category" column from 'categories' where id<4.
SELECT (SELECT COUNT(*) FROM `links` WHERE `visits`>14) AS nrl, `category` FROM `categories` WHERE `id`<4
Result:
| nrl | category  |
-------------------
|  2  | PHP-MySQL |
|  2  |   HTML    |

- There is also another way to select columns from two tables in a single query, by using the JOIN command. This method is presented in the next tutorial.

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag renders as emphasized text, displaying the text oblique?
<strong> <pre> <em>
<p>Web development courses: <em>CoursesWeb.net</em></p>
Which CSS property defines the space between the element border and its content?
margin padding position
h3 {
  padding: 2px 0.2em;
}
Click on the method which returns the first element that matches a specified group of selectors.
getElementsByName() querySelector() querySelectorAll()
// gets first Div with class="cls", and shows its content
var elm = document.querySelector("div.cls");
alert(elm.innerHTML);
Indicate the PHP variable that contains data from a form sent with method="post".
$_SESSION $_GET $_POST
if(isset($_POST["field"])) {
  echo $_POST["field"];
}
Select in two MySQL tables

Last accessed pages

  1. Keep the first Nr IMG tags, Strip all the others (314)
  2. Select the Content of HTML Element (4522)
  3. Adding text with ActionScript 3 (5638)
  4. CSS cursor property - Custom Cursors (6372)
  5. Zodiac Signs PHP code (7271)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (458)
  2. CSS cursor property - Custom Cursors (68)
  3. Read Excel file data in PHP - PhpExcelReader (46)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (44)
  5. PHP Unzipper - Extract Zip, Rar Archives (41)