[英]How to Select max Value from 2 tables in MySQL
I have 2 tables in MySQL DB. 我在MySQL DB中有2个表。
Both the tables have a column as ID which is of type int(10) unsigned . 两个表都有一个列作为ID ,其类型为int(10)unsigned 。
Table1 has no data and Table2 has the ID as 24 . 表1没有数据,表2的ID为24 。
I am using the below query to get the max ID 我正在使用以下查询来获取最大ID
select max(ID) from
(
select IFNULL(max(ID),0) as ID from table1
UNION
select IFNULL(max(ID),0) as ID from table2
)temp;
I am expecting the value 24 but it gives 0. 我期望值为24,但它给出0。
Anything wrong in my query? 我的查询有什么问题吗? Please help. 请帮忙。
try this, 尝试这个,
SELECT IFNULL(MAX(ID), 0) ID
FROM
(
SELECT ID FROM table1
UNION ALL
SELECT ID FROM table2
) a
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.