简体   繁体   English

连接3个表的Sql查询

[英]Sql Query On Joining 3 tables

I created 3 table as follows 我创建了3张表,如下

Table Student1: 表学生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

Table Student2: 表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

Table Student3: 表学生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

Excepted Result Condition: 例外结果条件:

1)Select Records with unique "id" from combining 3 tables. 1)从3个表中选择具有唯一“ id”的记录。

2)Update Field "date" of corresponding record if status=1 of same record with same name,amount in any of the 3 Tables. 2)如果相同名称的同条记录的status = 1,则更新对应记录的字段“日期”,金额在3个表中的任何一个。

3)If 2 or more than 2 record after combining 3 tables with same "id" but other fields different(ie Same id but different name,amount,date) then add all them to final result but my appending 1,11,111 to them. 3)如果在将3个具有相同“ id”但其他字段不同的表合并后(即相同id但名称,金额,日期不同)将2个或2个以上记录组合在一起,则将它们全部添加到最终结果中,但我将1,11,111附加到最终结果中。

Final Result To be Expected: 预期的最终结果:

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 Fiddle 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')
;

Query: 查询:

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

Error: 错误:

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