Java 使用多个表实现连接
我有一个表,我想从中选择许多行,并使用JOIN将id从两个表转换为名称。我试过这个:Java 使用多个表实现连接,java,mysql,sql,jpa,jpql,Java,Mysql,Sql,Jpa,Jpql,我有一个表,我想从中选择许多行,并使用JOIN将id从两个表转换为名称。我试过这个: String hql = "select e.* from " + PaymentTransactions.class.getName() + " e " + " INNER JOIN " + Merchants.class.getName() + " m " + " ON e.id = m.merchant_id AND "
String hql = "select e.* from " + PaymentTransactions.class.getName() + " e "
+ " INNER JOIN " + Merchants.class.getName() + " m "
+ " ON e.id = m.merchant_id AND "
+ " INNER JOIN " + Terminals.class.getName() + " t "
+ " ON e.id = t.terminal_id "
+ " where e.created_at >= :start_date and e.created_at <= :end_date";
主表:
@Entity
@Table(name = "payment_transactions")
public class PaymentTransactions implements Serializable {
.....
@Column(length = 4)
private Integer merchant_id;
@Column(length = 4)
private Integer terminal_id;
......
}
带有附加数据的表:
@Entity
@Table(name = "merchants")
public class Merchants implements Serializable {
@Column(name = "id")
private int id;
@Column(length = 255)
private String name;
.........
}
@Entity
@Table(name = "terminals")
public class Terminals implements Serializable {
@Column(name = "id")
private int id;
@Column(length = 255)
private String name;
.........
}
我试过这个:
String hql = "select e.* from " + PaymentTransactions.class.getName() + " e "
+ " INNER JOIN " + Merchants.class.getName() + " m "
+ " ON e.id = m.merchant_id AND "
+ " INNER JOIN " + Terminals.class.getName() + " t "
+ " ON e.id = t.terminal_id "
+ " where e.created_at >= :start_date and e.created_at <= :end_date";
String hql=“从”+PaymentTransactions.class.getName()+“e”中选择e.*
+“内部联接”+Merchants.class.getName()+“m”
+“在e.id=m.merchant_id上,并且”
+“内部联接”+终端.class.getName()+“t”
+“在e.id=t.terminal\u id上”
+“其中e.created_at>=:start_date和e.created_at您只需将这些变量添加到查询中的列列表中:
String hql = "select e.*, m.name AS merchant, t.name AS terminal"
+ " FROM " + PaymentTransactions.class.getName() + " e "
+ " INNER JOIN " + Merchants.class.getName() + " m "
+ " ON e.id = m.merchant_id AND "
+ " INNER JOIN " + Terminals.class.getName() + " t "
+ " ON e.id = t.terminal_id "
+ " where e.created_at >= :start_date and e.created_at <= :end_date";
String hql=“选择e.*,m.name作为商户,t.name作为终端”
+“来自”+PaymentTransactions.class.getName()+“e”
+“内部联接”+Merchants.class.getName()+“m”
+“在e.id=m.merchant_id上,并且”
+“内部联接”+终端.class.getName()+“t”
+“在e.id=t.terminal\u id上”
+“其中e.created_at>=:开始日期和e.created_at使用作为
关键字,正如本例所示选择e.*,m.name作为商户
,其中商户
是alias@PeterPenzov我已经在查询中输入了列别名(m.name AS merchant
),这是否回答了您的问题?如果没有,你能提供更多的信息来帮助回答这个问题吗?否则,请考虑标记接受的答案(在上/下投票箭头下的复选标记)。看见