我有 3 张表User, Product 和 Orders。
USER:
-----+-----------+------------+---------+----------+-----------+
u_id | username | password | contact | email | city |
------+-----------+------------+---------+---------+-----------+
PRODUCT:
+------+----------+--------------+------+--------------+
| p_id | category | listed_price | qty | description |
+------+----------+--------------+------+--------------+
ORDERS:
+------+--------+------+------+-----------+
| o_id | date | u_id | p_Id | order_qty |
+------+--------+------+------+-----------+
我想按以下顺序将 User 表与 Product 以及 o_id(来自 ORDER)一起内联:
+-----------+-------+------+------+--------------+
| username | email | p_id | o_id | listed_price |
+-----------+-------+------+------+--------------+
我的 SQL 查询如下:
productList = (List<Object[]>) session.createSQLQuery(
"SELECT User.username, User.email, Orders.p_id, Orders.o_id, Product.listed_price " +
"FROM Orders " +
"INNER JOIN User " +
"ON User.u_id = Orders.u_id" +
"INNER JOIN Product " +
"ON Product.p_id = Orders.p_id " +
"WHERE Product.p_id = '"+p_id +"' " +
"ORDER BY User.username").list();