2

我有 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();
4

2 回答 2

0

它会返回List<Object[]>。(对象数组列表),其中每个数组代表结果中的一行。值索引基于您的 select 语句。

在这里,您正在选择以下内容:

User.username, User.email, Orders.p_id, Orders.o_id, Product.listed_price

因此,您将在每个对象数组中获得 5 个元素。其中第一个元素是用户名,第二个元素是电子邮件,依此类推......

于 2013-02-26T07:30:43.327 回答
0

根据您的查询"SELECT User.username, User.email, Orders.p_id, Orders.o_id, Product.listed_price",它将返回 List<Object[]>.

于 2013-02-26T07:38:10.333 回答