How to check the existence of two columns' data in two different tables ? MySQL

前端 未结 2 1147
轻奢々
轻奢々 2021-01-17 06:54

I have two different tables in two different databases ..

what I want to do is to check if the data of two columns exist in the other table .. if it does exist count

相关标签:
2条回答
  • 2021-01-17 07:03

    If I'm understanding you correctly, one option is to use exists:

    select * 
    from table1 t1
    where exists (
        select 1
        from table2 t2 
        where t1.column1 = t2.column1 and
              t1.column2 = t2.column2
    )
    

    This will return a list of rows from the first table that have a corresponding matching row in the second.

    0 讨论(0)
  • 2021-01-17 07:12

    Do an INNER JOIN on both tables:

    SELECT COUNT(*)
    FROM table_1 t1
    INNER JOIN table_2 t2
        ON t1.column_1 = t2.column_1
        AND t1.column_2 = t2.column_2
    
    0 讨论(0)
提交回复
热议问题