Lets say we have the following query
SELECT b.user_name, c.user_name, msg
FROM user_messages a
JOIN users b ON b.user_id = a.user_from
JOIN users c ON c.user_id = a.user_to
WHERE a.user_from = 'ds4EpcrmUt'
OR a.user_to = 'ds4EpcrmUt'
When querying this from NodeJS I will only receive one of the user_names because both returned values have the same name.
Is there some way to rename their returned values like the following?
b.user_name = user_from
c.user_name = user_to
Try below -
SELECT b.user_name as user_from, c.user_name as user_to, msg
FROM user_messages a
JOIN users b ON b.user_id = a.user_from
JOIN users c ON c.user_id = a.user_to
WHERE a.user_from = 'ds4EpcrmUt'
OR a.user_to = 'ds4EpcrmUt'
When you refere to same column name you need different column alias name eg user_name1 and user_name1 or the column name alias you prefer :
SELECT b.user_name user_name1, c.user_name user_name2, msg
FROM user_messages a
JOIN users b ON b.user_id = a.user_from
JOIN users c ON c.user_id = a.user_to
WHERE a.user_from = 'ds4EpcrmUt'
OR a.user_to = 'ds4EpcrmUt'
SELECT b.user_name as user_from, c.user_name as user_to, msg
FROM user_messages a
JOIN users b ON b.user_id = a.user_from
JOIN users c ON c.user_id = a.user_to
WHERE a.user_from = 'ds4EpcrmUt'
OR a.user_to = 'ds4EpcrmUt'
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.