Ronnie Rocket
Ronnie Rocket

Reputation: 11

Querying on both columns in one town of a table

I have in my head what should be a simple thing to do but can't seem to find the answer.

I am given a list from a client e.g name timestamp

I want to put these in a temporary table then query them in turn e.g.

select reading where reading_table 
where name = t.c1 row1 and timestamp =t.c2 row1

and then just loop round each entry in the table

I could just hard code each entry from the client file as a select but thought there should be a better way of doing it as formatting is a problem when hardcoding and there is potential no rows found for some of them.

e.g.

select reading where reading_table 
where name = row1 name and timestamp = row1 timestamp
select reading where reading_table 
where name = row2 name and timestamp = row2 timestamp

. . .

select reading where reading_table 
where name = lastrow name and timestamp = lastrow timestamp

If it was just one column I would just use an IN but not sure what do 2 columns.

Upvotes: 0

Views: 47

Answers (1)

Lukasz Szozda
Lukasz Szozda

Reputation: 175796

Normally you should use JOIN operator. But you were very close anyway:

If it was just one column I would just use an IN but not sure what do 2 columns.

IN supports multiple columns.

SELECT reading 
FROM reading_table 
WHERE (name, timestamp) IN (SELECT name, timestamp FROM client);

Warning! This will work only if both columns are defined as NOT NULL.

Upvotes: 1

Related Questions