I know the question is too long but i cant describe it simple enough. So here is my case:
I want to select two or more rows in a same table in mysql with one of their field has the same value but will vary on the other field.
Example:
mainthread | category
1234 1
1234 1
1234 3
1234 3
5643 4
4322 6
3123 9
now i want all the category of 1 will be updated to 3 when they have the same mainthread value. The category i want to change is always from 1 to 3. It's the mainthread that will vary cause there's like thousands of different mainthreads
Maybe I'm misunderstanding the question, but isn't this just:
Update mytable
Set category = 3
Where mainthread = 1234
If you want to update the table, to set the value of category
to be the same on all the rows with the same value for mainthread
, then
UPDATE mytable t
JOIN ( SELECT mainthread
, MAX(category) AS max_category
FROM mytable
GROUP BY mainthread
) s
ON s.mainthread = t.mainthread
SET t.category = s.max_category
If you just want to return rows that have the same mainthread value with two or more different values for category, then:
SELECT t.*
FROM mainthread t
JOIN ( SELECT mainthread
FROM mytable
GROUP BY mainthread
HAVING COUNT(DISTINCT category) > 1
) s
ON s.mainthread = t.mainthread
ORDER
BY t.mainthread
, t.category
(It's not entirely clear from your question what you are trying to accomplish.)
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.