我将如何更正此SQL / MYSQL完全外部联接问题?

时间:2019-01-31 08:54:01

标签: mysql sql

df1 <- structure(list(Var1 = c("SJDJWK", "ODJSMDK", "JDKSAKDJ"), 
  Var2 = c("P04TGI7F3;P030Y7Y11;PE35RV747;Q2UKLVVX4", 
"Q2UKLVVX4;PWER00711;PE35RV747;Q2UKLVVX4", "PE35RV747;P0F071G1G"
)), class = "data.frame", row.names = c("1", "2", "3"))

df2 <- structure(list(Var_x = c("P04TGI7F3", "P030Y7Y11", "PE35RV747", 
"Q2UKLVVX4"), Var_y = c("good", "normal", "bad", "normal")), 
     class = "data.frame", row.names = c("1", 
"2", "3", "4"))

我的查询先生似乎是什么问题?

它显示此错误:

  

1064-您的SQL语法有错误;检查与您的MariaDB服务器版本相对应的手册以使用正确的语法   在'FULL OUTER JOIN Leaders附近   选民infotbl.lfname = leaderstbl.lfname   第3行的l'分组

我遇到麻烦了,有人可以帮助我吗?

PS:这是我的顶峰 谢谢大家的帮助!

2 个答案:

答案 0 :(得分:1)

mysql没有完全外部联接,您可以执行两次左联接或左右联接和联合,它们实际上就像完全外部联接一样

select col1..coln from table1 t1 left join table2 t2 on t1.col=t2.col
union
select col1..coln from table2 t2 left join table1 t1 on t1.col=t2.col

select col1..coln from table1 t1 left join table2 t2 on t1.col=t2.col
union
select col1..coln from table1 t1 right join table2 t2 on t1.col=t2.col

答案 1 :(得分:1)

您在FULL OUTER JOIN之前使用WHERE子句。试试这个:

SELECT leaderstbl.lfname                             AS leaders,
       IF(COUNT(vfname) > 1, COUNT(vfname), 0)       AS followers,
       IF(COUNT(household) > 1, COUNT(household), 0) AS household
FROM votersinfotbl
FULL OUTER JOIN leaderstbl ON votersinfotbl.lfname=leaderstbl.lfname
WHERE vleaders IN (SELECT vleaders FROM votersinfotbl WHERE vaddress = 'ABACA')
  AND vleaders NOT IN ('NONE', 'none', 'None', '')
  AND vaddress = 'ABACA'
  AND household IN (SELECT vleaders FROM votersinfotbl WHERE vaddress = 'ABACA')
GROUP BY leaderstbl.lfname
ORDER BY vleaders ASC