2012-10-15 94 views
2

表:用戶MySQL的來自具有不同的表選擇如果子句

| userid | fullname | 
| 1 | admin | 
| 2 | jimmy | 
| 3 | anton | 
| 4 | steven | 

表:MASCHINEN

| maschine | maschine-name | 
| 1  |  M1  | 
| 2  |  M2  | 
| 3  |  M3  | 
| 4  |  M4  | 

表:vorrichtungen

| id | maschine | vorrichtung | createdby |  datecreated | updatedby |  dateupdated  | 
| 1 | 1  | 11111 |  1  | 2012-06-23 17:10:25 |  1  | 2012-06-23 18:15:20 | 
| 2 | 1  | 22222 |  1  | 2012-06-25 07:50:49 |  0  | 0000-00-00 00:00:00 | 
| 3 | 2  | 33333 |  2  | 2012-06-27 10:22:29 |  0  | 0000-00-00 00:00:00 | 
| 4 | 3  | 44444 |  2  | 2012-06-28 09:41:27 |  1  | 2012-06-28 12:40:37 | 
| 5 | 4  | 11111 |  3  | 2012-06-29 15:53:47 |  0  | 0000-00-00 00:00:00 | 
| 6 | 4  | 33333 |  4  | 2012-06-29 22:30:02 |  2  | 2012-06-29 22:30:02 | 

我的查詢看起來像這樣:

SELECT 
    vorrichtungen.`id`, 
    maschinen.`maschine-name` AS maschine, 
    vorrichtungen.`vorrichtung`, 
    vorrichtungen.`createdby`, 
    user.`fullname` AS creator, 
    vorrichtungen.`datecreated`, 
    vorrichtungen.`updatedby`, 
    vorrichtungen.`dateupdated` 
FROM 
    vorrichtungen,maschinen,user 
WHERE 
    maschinen.`maschine` = vorrichtungen.`maschine` 
AND 
    vorrichtungen.`createdby` = user.`userid` 

,我收到這樣的結果:

| id | maschine | vorrichtung | createdby | creator |  datecreated | updatedby |  dateupdated  | 
| 1 | M1 | 11111 |  1  | admin | 2012-06-23 17:10:25 |  1  | 2012-06-23 18:15:20 | 
| 2 | M1 | 22222 |  1  | admin | 2012-06-25 07:50:49 |  0  | 0000-00-00 00:00:00 | 
| 3 | M2 | 33333 |  2  | jimmy | 2012-06-27 10:22:29 |  0  | 0000-00-00 00:00:00 | 
| 4 | M3 | 44444 |  2  | jimmy | 2012-06-28 09:41:27 |  1  | 2012-06-28 12:40:37 | 
| 5 | M4 | 11111 |  3  | anton | 2012-06-29 15:53:47 |  0  | 0000-00-00 00:00:00 | 
| 6 | M4 | 22222 |  4  | steven | 2012-06-29 22:30:02 |  2  | 2012-06-29 22:30:02 | 

我試圖解釋我想要什麼..

如果updatedby = 0比從表用戶AS更新其他選擇全名0或類似的東西..但我想所有6行

我希望收到這樣的結果:

| id | maschine | vorrichtung | createdby | creator |  datecreated | updatedby | updatetor |  dateupdated  | 
| 1 | M1 | 11111 |  1  | admin | 2012-06-23 17:10:25 |  1  | admin | 2012-06-23 18:15:20 | 
| 2 | M1 | 22222 |  1  | admin | 2012-06-25 07:50:49 |  0  |  0 | 0000-00-00 00:00:00 | 
| 3 | M2 | 33333 |  2  | jimmy | 2012-06-27 10:22:29 |  0  |  0 | 0000-00-00 00:00:00 | 
| 4 | M3 | 44444 |  2  | jimmy | 2012-06-28 09:41:27 |  1  | admin | 2012-06-28 12:40:37 | 
| 5 | M4 | 11111 |  3  | anton | 2012-06-29 15:53:47 |  0  |  0 | 0000-00-00 00:00:00 | 
| 6 | M4 | 22222 |  4  | steven | 2012-06-29 22:30:02 |  2  | jimmy | 2012-06-29 22:30:02 | 

是有可能實現嗎? 感謝您的幫助:)

問候貝爾尼

回答

2
SELECT 
    v.`id`, 
    m.`maschine-name` AS maschine, 
    v.`vorrichtung`, 
    v.`createdby`, 
    v.`datecreated`, 
    creator.fullname as creator, 
    v.`updatedby`, 
    case when v.updatedby is null then '0' else updator.`fullname` end AS updatetor, 
    v.`dateupdated` 
from 
    vorrichtungen v 
    inner join 
    user creator on creator.userid = v.createdby 
    left join 
    user updator on updator.userid = v.updatedby 
    inner join 
    maschinen m on m.maschine = v.maschine 
order by datecreated 
+0

我不會假設用戶在u.userid = v.updatedby上,其中0在內連接中可用。一個左連接+測試null和0是否更好? –

+0

嗨Clodoaldo ...同樣在這裏..我只收到ID 1,4,6 ...但我想要所有:D – bernte

+0

@bernte嘗試編輯的版本。 –

1

這將返回你的東西更接近你想要什麼。

SELECT 
vorrichtungen.`id`, 
maschinen.`maschine-name` AS maschine, 
vorrichtungen.`vorrichtung`, 
vorrichtungen.`createdby`, 
creatortable.`fullname` AS creator, 
vorrichtungen.`datecreated`, 
vorrichtungen.`updatedby`, 
updatetortable.`fullname` AS updatetor , 
vorrichtungen.`dateupdated` 
FROM 
vorrichtungen, 
maschinen, 
user as creatortable , 
user as updatetortable 
WHERE 
vorrichtungen.createdby = creatortable.userid 
AND 
vorrichtungen.updatedby = updatetortable.userid 
AND 
vorrichtungen.id  = maschinen.maschine