I have a MySQL query:
SELECT *
FROM t1
WHERE ABS(TIMESTAMPDIFF(MINUTE, ts, (SELECT ts FROM t1 WHERE id=1 AND c1 < 5))) < 3
AND id=1
The subquery returns 4 rows so the query cannot executed. I have change it to use join:
SELECT *
FROM t1 a INNER JOIN t1 b
ON a.id=b.id AND b.id=1 AND c1<5
WHERE ABS(TIMESTAMP(MINUTE, a.ts, b.ts))<3;
I have 2 questions:
1. Did I convert (from subquery to join) right?
2. Anyway to fix my subquery?
Thanks.
1.
It’s not valid, as you have to prefix all your fields (maybe a typo), or you’ll have ambiguity errors. And other typos (
TIMESTAMPinstead ofTIMESTAMPDIFF)By the way, in your case, you can put the predicate condition in the where clause, not in the join.
2.
Maybe an EXISTS clause could do the job.