我有 3 个表 table_1、table_2 和 table_3,它们的公共(public)列 comm_name 作为外键。我只想通过连接这 3 个表来找到列 data_id 的最大值。使用 where 条件 comm_name
简而言之:合并这三个表并找到最大的 data_id ,即:返回 9
我试过:
SELECT max(data_id) FROM (( SELECT table_1.data_id FROM table_1 where comm_name='aa') UNION(SELECT table_2.data_id FROM table_2 where comm_name='aa') UNION(SELECT table_3.data_id FROM table_2 where comm_name='aa'));
但是显示错误
An expression was expected. (near "(" at position 26)
Unexpected token. (near "(" at position 26)
Unexpected token. (near "(" at position 27)
This type of clause was previously parsed. (near "SELECT" at position 29)
This type of clause was previously parsed. (near "SELECT" at position 125)
This type of clause was previously parsed. (near "SELECT" at position 220)
最佳答案
联合运算符应该可以帮助您拥有 1 个可以查询的数据集(未测试):
select max(data_id) from (select data_id from table_1 union select data_id from table_2 union select data_id from table_3)
关于php - 连接表并找到列值的最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38539773/