簡體   English   中英

連接3個表的Sql查詢

[英]Sql Query On Joining 3 tables

我創建了3張表,如下

表學生1:

id  status  amount    Name            date
1     0      4500     ram           04/02/2012
2     0      2000    shyam          05/09/2013
4     0      1500    ghanshyam      08/11/2014

表Student2:

id   status   amount    Name          date
3      0      4500     gopal       04/02/2012
2      0      8000   radheshyam    15/11/2013
4      1      1500    ghanshyam    18/10/2015

表學生3:

id   status   amount    Name          date
1      1      4500     ram         04/02/2012
2      0      6500     radhe       11/11/2014
3      1      4500     gopal       04/02/2012

例外結果條件:

1)從3個表中選擇具有唯一“ id”的記錄。

2)如果相同名稱的同條記錄的status = 1,則更新對應記錄的字段“日期”,金額在3個表中的任何一個。

3)如果在將3個具有相同“ id”但其他字段不同的表合並后(即相同id但名稱,金額,日期不同)將2個或2個以上記錄組合在一起,則將它們全部添加到最終結果中,但我將1,11,111附加到最終結果中。

預期的最終結果:

id  status  amount    Name            date
1     1      4500     ram           04/02/2012
2     0      2000    shyam          05/09/2013
21    0      8000   radheshyam      15/11/2013
211   0      6500     radhe         11/11/2014
3     1      4500     gopal         04/02/2012
4     1      1500    ghanshyam      18/10/2015

SQL小提琴

CREATE TABLE Student1
    (`id` int,`status` int,`amount` int , `Name` varchar(10), `date` varchar(55))
;

INSERT INTO Student1
    (`id`,`status`,`amount`, `Name`, `date`)
VALUES
    (1,0,4500, 'ram', '04/02/2012'),
    (2,0,2000, 'shyam', '05/09/2013'),
    (4,0,1500, 'ghanshyam', '08/11/2014')
;

CREATE TABLE Student2
    (`id` int,`status` int,`amount` int , `Name` varchar(10), `date` varchar(55))
;

INSERT INTO Student2
    (`id`,`status`,`amount`, `Name`, `date`)
VALUES

    (3,0,4500, 'gopal', '04/02/2012'),
    (2,0,8000, 'radheshyam', '15/11/2013'),
    (4,1,1500, 'ghanshyam', '18/10/2015')
;

CREATE TABLE Student3
    (`id` int,`status` int,`amount` int , `Name` varchar(10), `date` varchar(55))
;

INSERT INTO Student3
    (`id`,`status`,`amount`, `Name`, `date`)
VALUES

    (1,1,4500, 'ram', '04/02/2012'),
    (2,0,6500, 'radhe', '11/11/2014'),
    (3,1,4500, 'ghanshyam', '04/02/2012')
;

查詢:

 SELECT * FROM Student1
FULL OUTER JOIN Student2
ON Student1.Name = Student2.Name

錯誤:

check the manual that corresponds to your MySQL server version for the right syntax to use near 'OUTER JOIN Student2 ON Student1.Name = Student2.Name' at line 2

這只是這三個表之間的聯合。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM