In MySQL, how can I find all rows whose attribute1
is the same as a particular row's attribute1
? I thought about doing
SELECT
t1.id
FROM
t AS t1
, t AS t2
WHERE
t2.id=123
AND t1.a=t2.a;
but it has been running for eons.
This should work to return the required rows.
SELECT t1.id
FROM t AS t1
JOIN t AS t2 ON (t1.a = t2.a and t1.id <> t2.id)
WHERE t2.id=123;
How many rows are in your table? Is the "a" column indexed? Adding an index should speed up the join.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.