如果两个表中的某个日期相同,如何从两个表中返回一条记录

z4bn682m  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(298)

我有两个表,每个表都有一个日期字段。如果任一表都有特定的日期,则只返回对应表中的单个记录。这很接近,但它总是返回两个表:

SELECT ruberic.teacherid,
       ruberic.date,
       coaching_interactions.teacherid,
       coaching_interactions.date
FROM   ruberic,
       coaching_interactions
WHERE  ruberic.date = '2016-12-13'
        OR coaching_interactions.date = '2016-12-13'
7hiiyaii

7hiiyaii1#

你可以用 Union . 它还将删除重复项(如果有):

SELECT ruberic.teacherid,
       ruberic.date,
FROM   ruberic 
WHERE  ruberic.date = '2016-12-13'

UNION 

SELECT coaching_interactions.teacherid,
       coaching_interactions.date
FROM   coaching_interactions
WHERE  coaching_interactions.date = '2016-12-13'

相关问题