Skip to content
Advertisement

Selecting all data from one table where conditions are met in another table

Say I have two tables:

Table enterprise:

  • id > integer
  • name > text
  • created_on > date

Table contact:

  • id > integer
  • enterprise_id > integer (matching from table Prime)
  • first_name > text
  • last_name > text
  • created_on > date

I want to select all contacts of enterprises, created after January 1, 2010. What I have so far is Select contact.* FROM `contact` WHERE created_date < 01012010 00:00:00 but I don’t think this checks the enterprise table?

Advertisement

Answer

In order to check the other table, too, you could use JOIN (INNER JOIN in this case).

The general format for that is this:

SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;

Or specifically for your question:

SELECT contact.*
FROM contact
INNER JOIN enterprise ON contact.enterprise_id = enterprise.id
WHERE contact.created_on > 01012010 00:00:00 
AND enterprise.created_on > 01012010 00:00:00

To actually check the other table you might also want to elaborate the WHERE statement. For further explanations and especially examples, you might want to visit w3schools.com; they got plenty of help for SQL.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement