Home > Net >  How to check if id occurs in other table in MySQL
How to check if id occurs in other table in MySQL

Time:08-05

I have a table an_visit that has column visit_id. There is also a table an_transaction that has some visit_id too.

I would like to get a new column in MySQL based on if visit_id occurs in both tables. My approach:


SELECT visit_id, datetime_add, ISNULL((SELECT   
                1
                FROM an_transaction
                WHERE an_transaction.visit_id = an_visit.visit_id), 0)
  
            FROM an_visit
            WHERE datetime_add >= '2021-08-01'
            LIMIT 50

But I got this error: MySQLdb.OperationalError: (1582, "Incorrect parameter count in the call to native function 'ISNULL'"). What do I do wrong, please?

CodePudding user response:

SELECT visit_id, datetime_add, ISNULL((SELECT   
                1
                FROM an_transaction
                WHERE an_transaction.visit_id = an_visit.visit_id limit 1))
  
            FROM an_visit
            WHERE datetime_add >= '2021-08-01'
            LIMIT 50

OR

SELECT visit_id, datetime_add, IFNULL((SELECT   
                1
                FROM an_transaction
                WHERE an_transaction.visit_id = an_visit.visit_id limit 1),0)
  
            FROM an_visit
            WHERE datetime_add >= '2021-08-01'
            LIMIT 50
  • Related