mysql - 如何在 SQL 中获取具有两个对应列的记录?

标签 mysql sql database

例如,我有两个表:

ID | Name
------------
1  | test 1 
2  | test 2 

ID2| ID | Age
--------------
1  | 1  | 18
2  | 1  | 18
3  | 1  | 19
4  | 2  | 18
5  | 2  | 19

我想要所有具有名称和年龄多个列的记录,但我不知道该怎么做。

我想要这样的输出:

Name     | Age
--------------------
test 1   | 18
test 1   | 18

有人可以帮助我吗?

最佳答案

尝试以下查询:

Select t1.*, t2.* 
from table1 t1
join table2 t2
on  t1.id = t2.id
join (select id, age
      from table2
      group by id, age
      having count(*) > 1
     ) t3
on t1.id = t2.id and t2.id = t3.id and t2.age = t3.age

关于mysql - 如何在 SQL 中获取具有两个对应列的记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54618589/

相关文章:

php - SQLSTATE[HY093] : Invalid Parameter number - PDO Select statement

mysql - 1292 列 'updated_at' 的日期时间值不正确

c# - 安全字符串格式对象 C#

SQL 查询缺少运算符

php - 数据库选择题和评论结构

java - 有人有过使用 JODB 进行网络应用程序的经验吗?

mysql - 使用 where 子句将数据表复制到表

php - 根据搜索字符串出现次数对数组进行排序 php

sql - 数据库对象中的封装查询返回的行数太少

database - BoltDB 作为后端数据库的性能