我有两个桌子
表A
+-----+---------+---------+
| pid | name | country |
+-----+---------+---------+
| 1 | ABC | XXXXXXX |
| 2 | EFG | YYYYYYY |
| 3 | IJK | ZZZZZZZ |
| 4 | LMN | AAAAAAA |
| 5 | OPQ | BBBBBBB |
| 6 | RST | CCCCCCC |
+-----+---------+---------+
表B
+-----+---------+
| id | pid |
+-----+---------+
| 5 | 5 |
| 4 | 1 |
| 3 | 2 |
| 2 | 5 |
| 1 | 2 |
+-----+---------+
我想要下面的输出
+-----+---------+---------+
| pid | name | country |
+-----+---------+---------+
| 5 | OPQ | BBBBBBB |
| 1 | ABC | XXXXXXX |
| 2 | EFG | YYYYYYY |
+-----+---------+---------+
这是输出应获取表B id字段的最新条目,并且表A的数据应显示在表B的pid出现顺序上
答案 0 :(得分:2)
尝试一下:
SELECT DISTINCT A.PID, A.NAME, A.COUNTRY
FROM TABLE_A A
INNER JOIN TABLE_B B
ON A.PID = B.PID
ORDER BY A.PID
答案 1 :(得分:1)
您需要将表连接在一起并获取最新值。这是一种方法:
select a.pid, a.name, a.country
from a join
b
on a.pid = b.pid
where b.id = (select max(b2.id) from b b2 where b2.pid = b.pid);
答案 2 :(得分:0)
您不需要B的联接或字段吗?
SELECT a.pi, a.name, a.country
FROM A AS a
WHERE EXISTS(SELECT b.id FROM B AS b WHERE b.pid = a.pid)
答案 3 :(得分:0)
您也可以使用class currencyAccounts: Object {
@objc dynamic var currencyID : Int = 0
@objc dynamic var currencyName = ""
@objc dynamic var currencyCode = ""
@objc dynamic var currencyBalance : Double = 0.00
@objc dynamic var currencyStatusCode : Bool = false
override static func primaryKey() -> String? {
return "currencyID"
}
convenience init(currencyID: Int, currencyName: String, currencyCode: String, currencyStatusCode: Bool, currencyBalance: Double) {
self.init()
self.currencyID = currencyID
self.currencyName = currencyName
self.currencyCode = currencyCode
self.currencyStatusCode = currencyStatusCode
self.currencyBalance = currencyBalance
}
:
row_number()