ThP
ThP

Reputation: 185

How can you compare values from two columns in two separate tables?

I want to compare the last value in one column in a table with every value in the column of the other table. After many tries can't seem to be able to achieve it.

Something like that: In this case I want to compare Value 3 with every value in column 4.

Table_1:
+---------+---------+
| column1 | column2 |
+---------+---------+
| value1  | value4  |
| value2  | value5  |
| value3  | value6  |
+---------+---------+


Table_2
+---------+---------+
| column3 | column4 |
+---------+---------+
| value7  | value10 |
| value8  | value11 |
| value9  | value12 |
+---------+---------+

Upvotes: 1

Views: 243

Answers (1)

ScaisEdge
ScaisEdge

Reputation: 133370

You can use a subselect using max

select * from Table_2
where  Table_2.column4 = (select max(column1) from table_1 )

or using order by

select * from Table_2
where  Table_2.column4 = (select column1 from table_1 order by column1 DESC limit 1)

or using max(id)

select * from Table_2
where  Table_2.column4 = (select column1 from table_1 having id = max(id))

Upvotes: 1

Related Questions