【问题标题】:Convert a complex relational SQL query so that it runs in OrientDB转换复杂的关系 SQL 查询,使其在 OrientDB 中运行
【发布时间】:2016-05-07 12:17:08
【问题描述】:

我是 OrientDB 的新手。我有一个表结构,如下所示。我已经尝试了很多查询,但仍然无法弄清楚如何在 OrientDB 中实现以下 SQL 查询。我需要加入来自 4 个不同类的数据。非常感谢任何帮助。

select Users.Name as uName,
   Shops.Name as shopName,
   ShopTotalPurchases.Total

from Users, Shops, Purchases,ShopTotalPurchases 

where Users.Id=10 

and Purchases.UserId=Users.Id

and Purchases.Date="date2"

and ShopTotalPurchases.Date="date2"

and ShopTotalPurchases.ShopId = Shops.Id

> reldb> SELECT * FROM Users;
+----+--------+-------+
| Id | Name   | Phone |
+----+----------------+
| 10 | User 1 | 1111  |
+----+----------------+
| 20 | User 2 | 2222  |
+----+----------------+


> reldb> SELECT * FROM Shops;
+----+--------+-------+
| Id | Name   | Phone |
+----+----------------+
| 30 | Shop 1 | 1111  |
+----+----------------+
| 40 | Shop 2 | 2222  |
+----+----------------+

> reldb> SELECT * FROM Purchases; [unique per combo of UserId, ShopId and Date]
+----+--------+--------+--------+------+-----------+
| Id | UserId | Item   | ShopId | Date | ItemPrice |
+----+--------+-----------------+------+-----------+
|  0 | 10     | First  |  30    |date1 |   100     |
+----+--------+--------+--------+------+-----------+
|  1 | 10     | Second |  30    |date2 |   200     |
+----+--------+--------+--------+------+-----------+
| 21 | 10     | Third  |  40    |date3 |   300     |
+----+--------+--------+--------+------+-----------+
| 41 | 20     | Fourth |  40    |date4 |   400     |
+----+--------+--------+--------+------+-----------+
| 82 | 20     | Fift   |  30    |date5 |   500     |
+----+--------+--------+--------+------+-----------+

> reldb> SELECT * FROM ShopTotalPurchases;
+----+--------+--------+------+
| Id | Total  | ShopId | Date |
+----+--------+--------+------+
|  0 | 1000   |  30    |date1 |
+----+--------+--------+------+
|  1 | 2000   |  30    |date2 |
+----+--------+--------+------+
| 21 | 3000   |  40    |date2 |
+----+--------+--------+------+

【问题讨论】:

    标签: sql join orientdb translate orientdb-2.1


    【解决方案1】:

    你可以使用这个查询:

    select $a.Name as uName, $b.Name as shopName, $c.Total as Total from Purchases
    let $a = (select Name from User where Id = 10 and Id in $parent.current.UserId),
        $b = (select Name from Shops where Id in $parent.current.ShopId),
        $c = (select Total from ShopTotalPurchases where Date = 'date2' and ShopId in $parent.current.ShopId)
    where Date = 'date2' unwind uName, shopName, Total
    

    控制台输出

    ----+------+-----+--------+------
    #   |@CLASS|uName|shopName|Total
    ----+------+-----+--------+------
    0   |null  |User1|Shop1   |2000.0
    ----+------+-----+--------+------
    

    【讨论】:

    • 您好,您有机会尝试查询吗?
    猜你喜欢
    • 1970-01-01
    • 2021-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-03
    • 1970-01-01
    • 2017-08-15
    • 1970-01-01
    相关资源
    最近更新 更多