Select data from multiple tables and use aliases

code

SELECT p.*, c.ID, c.Description as CategoryName
    FROM Products p, Categories c
    where p.CategoryID=c.ID

Explanation

This will select everything from products table and only ‘ID’ and ‘Description’ from categories table given the category IDs are same. Note that the alias for ‘Products’ table is p and Alias for ‘Categories’ table is c. Column ‘Description’ of categories table will be shown as ‘CategoryName’ in the resultant table as the alias for that column is ‘CategoryName’. The aliases are only the temporary names that can be viewed when the query executes.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Create a free website or blog at WordPress.com.

Up ↑

Design a site like this with WordPress.com
Get started