user1563414
user1563414

Reputation: 91

Pull SQL data from two tables

I have a table called db.data and db.info. The db.data table has a name and dateadded column for every entry. The info table has a name and a status column for every name (status is either y or n). I would like to pull the data from the db.data table for all the names added to that db by date but only if the status (the one in db.status) is n.

I have been looking around and found JOIN, but that seems to only work if the data is the same in both tables. Here, the names are the same, but I want the data extracted from db.data but only in the status for the 'name' cell (which is in both tables) is set to 'n'.

Upvotes: 1

Views: 3180

Answers (3)

Pete B.
Pete B.

Reputation: 3276

I think this is what you want:

select d.name, d.dateadded
  FROM db.info i, db.data d 
  WHERE i.name = d.name
    AND i.status = 'n';

Upvotes: 1

Matt Busche
Matt Busche

Reputation: 14333

You can use a join on name between the two tables

SELECT d.name, d.date
FROM db.data d
    JOIN db.info i ON d.name = i.name
WHERE i.status = 'N'
ORDER BY d.date

Upvotes: 1

lc.
lc.

Reputation: 116448

From what I understand, the data in each name column is the same for both tables. So a JOIN should work here:

SELECT db.data.*
FROM db.data
INNER JOIN db.info ON db.data.name = db.info.name
WHERE db.info.status = 'n'

Upvotes: 1

Related Questions