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

时间:2021-11-14 22:57:39

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 |
+---------+---------+

1 个解决方案

#1


1  

You can use a subselect using max

您可以使用max来使用subselect

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)

或使用max(id)

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

#1


1  

You can use a subselect using max

您可以使用max来使用subselect

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)

或使用max(id)

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