-3

私は を初めて使用しsql、3 つのテーブルに参加するタスクを与えられました。誰かがこれに光を当ててくださいsql

表1のように3つのテーブルがあります

userID | username | password
 1        user1      user1
 2        user2      user2

表 2

userID | order number | order quantity
2           101             100

表 3

userID | name | address
1        John    xxx
2        Will    xxx
3        Peter   xxx

結果テーブルは次のように表示する必要があります。

UserID | username | name | address | order number | order quantity        
1         user1     John    xxxxx      0               0
2         user2     Will    xxxxx      101           100
3         user3     Peter   xxxxx      0               0
4

2 に答える 2

1

一部の列ではLEFT JOINwithを使用する必要があります。COALESCE

SELECT  a.`userid`,
        a.`username`,
        c.`name`,
        c.`address`,
        COALESCE(b.`order number`, 0) `order Number`,
        COALESCE(b.`order quantity`, 0) `order quantity`
FROM    table1 a
        LEFT JOIN table2 b
            on a.userid = b.userid
        LEFT JOIN table3 c
            on a.userid = c.userid

基本的にLEFT JOINは、2 番目のテーブルに一致するかどうかに関係なく、左側のテーブルからすべての行を取得します。値がどのように見えるCOALESCEかを処理します。null

于 2012-09-10T09:45:22.393 に答える
0

これは役に立ちます

select 
    a.userID, 
    c.name, 
    c.address, 
    b.order_number, 
    b.order_quantity 
from 
    table1 a 
        join table3 c 
            on a.userID=c.userID 
        join table2 b 
            on a.userID=c.userID

ただし、これは非常に簡単なことなので、SQL の本を手に入れる必要があるかもしれません。

于 2012-09-10T09:48:13.953 に答える