There are several approaches with differences in performance characteristics.
One option is to perform a correlated subquery. This approach is best if you have a suitable index and you occupy a relatively small number of rows.
SELECT t.id , t.name , t.payment_date , t.fee , t.amt , ( SELECT 'Y' FROM testing s WHERE s.name = t.name AND s.fee = t.fee AND s.amt = t.amt AND s.id <> t.id LIMIT 1 ) AS SameDataExist FROM testing t WHERE ... LIMIT ...
The correlated subquery in the SELECT list returns Y if at least one match string is found. If no match string is found, the SameDataExist column will be NULL. To convert NULL to "N", you can wrap the subquery in the IFULL () function.
Your method 2 is an acceptable approach. An expression in a SELECT list does not have to perform all these comparisons that have already been performed on join predicates. All you need to know is whether a matching row is found ... just check one of the columns for NULL / NOT NULL.
SELECT t.id , t.name , t.payment_date , t.fee , t.amt , IF(s.name IS NOT NULL,'Y','N') AS SameDataExists FROM testing t LEFT JOIN (
You can also use EXISTS (correlated subquery)
source share