有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java获取记录所在的mysql表的名称

我在MySQL中有6个表,它们是这样映射的。(一般化/专业化)

1.Expense
2.Administrative Expense
3.Vehicle Expense
4.Machine Expense
5.Salaries
6.Purchases

费用是超级类,其他是子类。费用表的主键“expenseId”被引用,是其他表的主键。ID的格式为“EXP001”

我想检查给定的ID(例如“EXP00010”)是管理费用还是车辆费用,或者是类似的费用。在这种情况下使用什么查询?我需要找到包含给定值的子类关系的名称

提前谢谢


共 (2) 个答案

  1. # 1 楼答案

    返回行时,选择表的名称作为常量:

    select * from (
      select 'Expense', * from expense
      union
      select 'Administrative Expense', * from Administrative_Expense
      union
        etc for all tables
    ) all
    where expenseId = ?   or join etc
    
  2. # 2 楼答案

    假设相同的费用ID不会出现在多个表中,下面的查询应该可以工作

    SELECT 
      a.count (*) AS admin,
      b.count (*) AS vehicle,
      c.count (*) AS machine 
    FROM
      admin_expense a,
      vehicle_expense b,
      machine_expense c 
    WHERE a.admin_expense_ID = 'EXP0001' 
      OR b.vehicle_expense_ID = 'EXP0001' 
      OR c.machine_expense_ID = 'EXP0001' ;
    

    问候,
    纳姆拉塔