sql用另一个表中的字符串替换多个ID

时间:2014-06-01 19:13:23

标签: python sql sqlite python-2.7

我的SQLite数据库包含四个数组:

CREATE TABLE IF NOT EXISTS EVENTS_LIST
(ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
DATE TEXT,
WORKER INTEGER,
EVENT INTEGER,
REGISTRATION_METHOD INTEGER)

CREATE TABLE IF NOT EXISTS WORKERS
(ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
WORKER TEXT)

CREATE TABLE IF NOT EXISTS EVENTS
(ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
EVENT TEXT)

CREATE TABLE IF NOT EXISTS REGISTRATION
(ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
REGISTRATION_METHOD TEXT)

我需要获取以下ID:工人,事件和注册在相应的表中替换为此ID附带的名称。

示例:

3 Workers: 0001) John 0002) Tom 0003) Mike
3 Types of registration: 0) PIN 1) Fingerprint 2) NFC Card
3 Types of events: 1) came in 2) came out 3) came out on business

我明白了:

DATE | WORKER | EVENT | REGISTRATION
XXXX |  0001  |   1   |      0
XXXX |  0003  |   2   |      1

我需要:

DATE | WORKER |    EVENT   | REGISTRATION
XXXX |  John  |   came in  |      PIN
XXXX |  Mike  |   came out |  Fingerprint

我找到了那些解决方案:

How to replace fetched values with another column while querying with SQL Server 2008 R2

Multiple column SQL joins in a table

第一个链接非常相​​似,但只与一列相关,第二个链接更复杂,但有一些“LEFT OUTER JOIN”命令,这似乎是一个很好的方向。

有人能告诉我如何做到这一点吗?

2 个答案:

答案 0 :(得分:0)

使用此代码:

select DATE=el.DATE,
   WORKER=w.wORKER,
   EVENT=e.EVENT,
   REGISTRATION=r.REGISTRATION
   from
   IF NOT EXISTS EVENTS_LIST as el 
   join 
   IF NOT EXISTS WORKERS as w on el.WORKER=w.ID
   join 
   IF NOT EXISTS EVENTS as e on el.EVENT=e.ID
   join
   IF NOT EXISTS REGISTRATION as r on el.REGISTRATION_METHOD=r.ID

答案 1 :(得分:0)

此代码对我有用:

select el.DATE,
   w.wORKER,
   e.EVENT,
   r.REGISTRATION
   from
   EVENTS_LIST as el 
   join 
   WORKERS as w on el.WORKER=w.ID
   join 
   EVENTS as e on el.EVENT=e.ID
   join
   REGISTRATION as r on el.REGISTRATION_METHOD=r.ID

谢谢!