从表1限制最多10个ID,并从表2中获取与表1的ID相匹配的所有记录

时间:2019-05-28 15:59:19

标签: mysql join select

例如,我有一个table1:

orderID userId orderName orderTime...
1         11
2.        12
3.        11
4.        14
5.        11
6.        13
7.        11
8.        15
9.        16
10.       11
...      ...

我还有另一个表table2:

table2ID orderID item    price ....
101       1       Apple   1.99
102       1       Banana  2.99
103       1       Grapes  0.99
104       4       pizza   6.99
105       4       drink   0.99
105.      3       chicken 1.99
106.      3       apple   1.99

我已经尝试过了:

SELECT a.*, b.* FROM `table1` a
    RIGHT JOIN table2 b on a.orderID = b.orderID 
    WHERE a.userID = 11 order by a.`orderTime` DESC LIMIT 25;

我想从用户11的表1中获取最多10个唯一的orderID,并从表2中获取这10个ID的所有详细信息。如果我执行LIMIT 25,那么我将无法获取所有信息。 我希望输出为:

orderID userId orderName orderTime... table2ID orderID item    price 
 1        11                            101       1    Apple   1.99
 1        11                            102       1    Banana 2.99
 1        11                            103       1    Grapes  0.99
 3        11                            105       3    chicken 1.99
 3        11                            106       3     apple   1.99

1 个答案:

答案 0 :(得分:0)

scala.collection.immutable.Map