感觉自己都已经转为数据库开发了,最近的项目主要是操作本地数据库,一个app有一百多张表,一会join 一会 left join
1 :五个表 scan ,result , intrument ,model, vender 表
现在想要查到 result表里面的 rfid ,model表里面的name, vender表里面的name
sql 语句(swift)
let sql = String(format:
" select RFIDScanSessionResult.Id," +
" RFIDScanSessionResult.rfid," +
" Instrument.building," +
" Instrument.room," +
" Instrument.customerDefineType," +
" Site.city," +
" vendorModelName," +
" standardizedName " +
" from ((select *from RFIDScanSessionResult" +
" left join RFIDScanSession on RFIDScanSessionResult.sessionId= RFIDScanSession.Id " +
" WHERE RFIDScanSessionResult.isHidden = 0 " +
" GROUP BY room,Site,building,RFIDScanSessionResult.rfid) as RFIDScanSessionResult " +
" left join Instrument on RFIDScanSessionResult.rfid = Instrument.rfidTag " +
" left join Model on Instrument.model = Model.Id " +
" left join Vendor on Model.vendor = Vendor.Id " +
" left join Site on Instrument.site = Site.Id) " +
" WHERE RFIDScanSessionResult.isHidden = 0 and " +
" (Instrument.isHidden = 0 or Instrument.isHidden is NULL) " +
" GROUP BY rfid having count(rfid) > 1 ");
2、 RFIDScanSessionResult表根据RFIDScanSessionResult.sessionId=RFIDScanSession.Id关联表RFIDScanSession 然后根据关联后的表根据RFIDScanSession.site= site.id关联表site,然后根据RFIDScanSessionResult.rfid = '%@' 查找关联后的数据
let sql = String(format:
" select %@,%@,%@,%@,%@ " +
" from RFIDScanSessionResult " +
" Join RFIDScanSession on " +
" RFIDScanSessionResult.sessionId = RFIDScanSession.Id " +
" JOIN Site on RFIDScanSession.site = site.id " +
" WHERE RFIDScanSessionResult.rfid = '%@' And RFIDScanSessionResult.isHidden = 0" , RFIDScanSessionResult.propertyNameSesssionId, RFIDScanSession.propertyNameBuilding, RFIDScanSession.propertyNameRoom,Site.propertyNameCity, RFIDScanSessionResult.propertyNameLastSeen,
rfid);