显示 MYSQL 表和/或 R 对象中存在的数据

发布于 2024-09-25 08:59:34 字数 210 浏览 3 评论 0原文

我认为这是一个常见问题,回答了好几次,但我只是不知道如何

在 MySQL 中提出正确的问题 =(:
我有 2 个表,里面有某种字符串,现在我想要:
1.两个表中都出现的数据
2. a中不在b表中的数据

在R中相同:
我有 2 个 R data.frame 并且我想要:
1.a和b中出现的数据
2. a中出现但b中不出现的数据

i think its an regular problem, answered several times, but I just don't know how to ask the question right =(

in MySQL:
i have 2 tables with some kind of strings inside, now i want:
1. the data that occur in both tables
2. the data from a that is not in table b

same in R:
i have 2 R data.frame s and i want:
1. the data that occur in a and b
2. the data that occur in a but not in b

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟雨扶苏 2024-10-02 08:59:34

在 R 中:

a <- data.frame(V1=sample(letters[1:3],20,TRUE),V2=rnorm(20))
b <- data.frame(V1=sample(letters[2:4],20,TRUE),V2=rnorm(20))

# the data that occur in a and b
(ab <- merge(a,b,by="V1"))

# the data that occur in a but not in b 
aNOTb <- merge(a,b,by="V1",all=TRUE)
(aNOTb <- aNOTb[is.na(aNOTb$V2.y),])

In R:

a <- data.frame(V1=sample(letters[1:3],20,TRUE),V2=rnorm(20))
b <- data.frame(V1=sample(letters[2:4],20,TRUE),V2=rnorm(20))

# the data that occur in a and b
(ab <- merge(a,b,by="V1"))

# the data that occur in a but not in b 
aNOTb <- merge(a,b,by="V1",all=TRUE)
(aNOTb <- aNOTb[is.na(aNOTb$V2.y),])
妞丶爷亲个 2024-10-02 08:59:34

在mysql中,您可以执行此操作来获取两个表中的数据

SELECT * FROM table1 t1 INNER JOIN table2 t2 ON t1.id = t2.id

,并执行此操作以从a中获取不在表b中的tada

SELECT * FROM table1 t1 LEFT JOIN table2 t2 ON t1.id = t2.id
WHERE t2.id IS NULL

in mysql you could do this to get the data in both tables

SELECT * FROM table1 t1 INNER JOIN table2 t2 ON t1.id = t2.id

and do this to get the tada from a that is not in table b

SELECT * FROM table1 t1 LEFT JOIN table2 t2 ON t1.id = t2.id
WHERE t2.id IS NULL
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文