简体   繁体   English

MySql - 如何使用 WHERE 子句从两个表中获取 select MAX ID

[英]MySql - How to select MAX ID from two tables with WHERE clause

I have two tables invoice and invoice_customer.我有两张表 invoice 和 invoice_customer。 Both have column invoice_id like (0000125) and firma_id (firma_id is a company_id) How can i select max invoice_id of two tables with using WHERE caluse?两者都有列 invoice_id,如 (0000125) 和firma_id(firma_id 是 company_id)我如何使用 WHERE caluse 对两个表进行 select max invoice_id ? i done query for single table only我只查询了单个表

SELECT MAX(invoice_id) AS maxID FROM invoice WHERE firma_id = 13

I done this way我是这样做的

SELECT MAX(invoice_id) maxID
FROM
(
  SELECT invoice_id FROM invoice WHERE firma_id = 13
  UNION ALL
  SELECT invoice_id FROM invoice_customers WHERE firma_id = 13
) test

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MySQL SELECT 使用 WHERE 子句查询不从两个表返回行 - MySQL SELECT Query with WHERE clause Not returning rows from two tables 如何根据where子句从两个表中进行选择 - How to select from two tables based on a where clause 来自2个不同表的MySQL SELECT,其中2个表具有WHERE子句,并且1个结果为NULL - MySQL SELECT from 2 different Tables with WHERE clause on 2 Tables and 1 result NULL 如何从MySQL中的2个表中选择最大值 - How to Select max Value from 2 tables in MySQL 从两个具有相同WHERE子句的不同表中进行选择? - SELECT FROM two different tables with the same WHERE clause? 在MYSQL WHERE子句中使用来自不同表的两个值的总和 - Use a sum of two values from different tables in MYSQL WHERE clause 从两个mysql表中选择,它们的列值相似并且id从上一页检索 - select from two mysql tables where column value is similar and id is retrieved from previous page 从两个表中的ID相同的两个表中选择数据 - Select Data from two tables where ID on both tables are same 如何使用 COUNT 和 WHERE 子句从两个不同的表中选择 2 个不同的列 - How to select 2 different columns from two different tables with COUNT and WHERE clause SELECT 来自两个表 WHERE 每个表中的不同列等于 $id ORDER BY 公共列(PHP / MySQL) - SELECT from two tables WHERE different columns in each table equal $id ORDER BY common column (PHP/MySQL)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM