php - 连接表并找到列值的最大值

标签 php mysql

我有 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)

enter image description here

最佳答案

联合运算符应该可以帮助您拥有 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/

相关文章:

javascript - 使用 Fancybox 的谷歌地图可以完美地运行 Firefox,但其他浏览器无法运行

javascript - 新浏览器选项卡或窗口中的 HTML

mysql - 从分隔文件加载数据到 mysql 表时跳过错误行

Mysql,在 JSON 字段中搜索?

PHP 作用域、$GLOBALS 和单例

JavaScript/PHP/HTML : Disabling respective textbox on unchecked checkbox

php - 给 mysqld_stmt_execute 的未知准备语句处理程序

mysql - Sql (mariadb) 删除重复名称并更新其他表

PHP MySQL 选择最大行数返回错误的数字

mysql - 缺少 JPA 实体概念?我是否错误地假设 JPA 条目将保存到我现有的数据库表中?