Table
|id |user_id | req_user_id |
|---|--------|-------------|
|1 | 1 | 2 |
|2 | 2 | 9 |
|3 | 7 | 2 |
When I search with user_id or req_user_id = 2 output should be (1,9,7,2)
tried
SET @list_req = (SELECT GROUP_CONCAT(DISTINCT CONCAT(user_id,',',req_user_id)) FROM send_request WHERE req_user_id= 2 or user_id=2);
output should be (1,9,7,2)
2
Answers
You can do:
Result:
See running example at db<>fiddle.
I see that @The Impaler’s answer is great.
This is an other way to do it using
JSON_TABLE
andJSON_OBJECTAGG
to extract distinct JSON array :Demo here