Friday, June 30, 2017

How to select two additional columns from another table based on values in the base table

Below is sample SQL showing how we can use additional columns from another table based on value of base table.


SELECT base_table.id, another_table.id AS some_other_name, another_table.name
FROM base_table AS base_table
  INNER JOIN (SELECT id, name FROM another_table) another_table
    ON base_table.another_table_id = another_table.id
WHERE another_table.some_reference = 15
ORDER BY base_table.id DESC
LIMIT 10


No comments:

Post a Comment