我是MYSQL的新手,想要通过UNION of 2创建一个表(Table_Result)给定表(Table1,2)。在表1,2中,“MEMORY”列中的一些值是相同的,但是相同存储器的计数可以相同或不同,如下所示。我想在“MEMORY”列的基础上创建新表(如table_result),该列具有最大的内存计数和前面的相对列。与其他表相比内存计数较少的表将使其他列(NAME,CONTENT或NAME1,CONTENT1)填充“NA”(参见table_result)。
任何想法如何编写Mysql查询脚本来获取结果。感谢你的帮助。
BR // RG
TABLE1
MEMORY NAME CONTENT
MEM 0110H Systemkennung DB01
MEM 0110H Hauptversion DB03
MEM 0110H Unterversion DB00
MEM 0110H Patchlevel DB04
MEM 0200H Steuerkanal1 DB01
MEM 0200H Steuerkanal2 DB01
MEM 0200H Steuerkanal3 DB01
MEM 0260H Steuerkanal35 DB01
MEM 0210H BUE-Gruppe DB01
MEM 0210H Licht-Gruppe1 DB01
TABLE2
MEMORY NAME1 CONTENT1
MEM 0110H System DB01
MEM 0110H Hauptversion1 DB02
MEM 0200H Steuerkanal11 DB01
MEM 0200H Steuerkanal22 DB06
MEM 0200H Steuerkanal34 DB05
MEM 0200H Steuerkanal35 DB04
MEM 0260H Steuerkanal36 DB02
MEM 0210H BUE-Gruppe DB01
TABLE_RESULT
MEMORY NAME CONTENT NAME1 CONTENT1
MEM 0110H Systemkennung DB01 System DB01
MEM 0110H Hauptversion DB03 Hauptversion1 DB02
MEM 0110H Unterversion DB00 NA NA
MEM 0110H Patchlevel DB04 NA NA
MEM 0200H Steuerkanal1 DB01 Steuerkanal11 DB01
MEM 0200H Steuerkanal2 DB01 Steuerkanal22 DB06
MEM 0200H Steuerkanal3 DB01 Steuerkanal34 DB05
MEM 0200H NA NA Steuerkanal35 DB04
MEM 0260H Steuerkanal35 DB01 Steuerkanal36 DB02
MEM 0210H BUE-Gruppe DB01 BUE-Gruppe DB01
MEM 0210H Licht-Gruppe1 DB01 NA NA
答案 0 :(得分:0)
SELECT
ifnull(a.MEMORY, b.MEMORY) AS MEMORY,
ifnull(a.NAME, 'NA') AS NAME,
ifnull(a.CONTENT, 'NA') AS CONTENT,
ifnull(b.NAME, 'NA') AS NAME1,
ifnull(b.CONTENT, 'NA') AS CONTENT1
FROM TABLE1 AS a
LEFT JOIN TABLE2 AS b ON a.MEMORY = b.MEMORY
UNION
SELECT
ifnull(a.MEMORY, b.MEMORY) AS MEMORY,
ifnull(a.NAME, 'NA') AS NAME,
ifnull(a.CONTENT, 'NA') AS CONTENT,
ifnull(b.NAME, 'NA') AS NAME1,
ifnull(b.CONTENT, 'NA') AS CONTENT1
FROM TABLE1 AS a
RIGHT JOIN TABLE2 AS b ON a.MEMORY = b.MEMORY
答案 1 :(得分:0)
答案如下:
conn.commit()