Problem:
I am trying to "translate" the id in two columns in a grading table (betyg_essays) to the real names of those users in another table (betyg_users).
The grading table (betyg_essays) look like this:
The user table (betyg_users) looks like this:
PHP code (so far):
$query = 'SELECT * FROM betyg_essays JOIN betyg_users ON betyg_essays.Examiner = betyg_users.UID';
Examiner/Supervisor columns (betyg_essays) correspond to the UID column (betyg_users).
Desired output should be (using the Firstname/Lastname columns):
Non-working SQL query:
$query = "SELECT
(u1.Firstname + ' ' + u1.Lastname) AS Examiner,
(u2.Firstname + ' ' + u2.Lastname) AS Supervisor
FROM betyg_essays grade
INNER JOIN betyg_users u1 ON grade.Examiner = u1.UID
INNER JOIN betyg_users u2 ON grade.Supervisor = u2.UID";
SELECT
CONCAT(u1.FirstName, ' ', u1.LastName) AS Examiner,
CONCAT(u2.FirstName, ' ', u2.LastName) AS Supervisor
FROM betyg_essays e
INNER JOIN betyg_users u1 ON e.Examiner = u1.UID
INNER JOIN betyg_users u2 ON e.Supervisor = u2.UID
I didn't tried the code, you might need to make some adjustments.
UPDATE: Fixed the id's
INNER JOIN betyg_users u2 ON e.Supervisor = u2.UID
UPDATE2:
I have changed the + to CONCAT function. Apparently, MySQL handles string differently then SQL Server.
Here you need to join user_table two times first for getting examiner user name and second for supervisor username
SELECT (g1.FirstName + ' ' + g1.LastName) AS Examiner,' , (g2.FirstName + ' ' + g2.LastName) AS Supervisor
FROM grading_table grade
INNER JOIN user_table g1 ON grade.examiner = g1.uid
INNER JOIN user_table g2 ON grade.supervisor = g2.uid
Final solution:
$query = "SELECT
CONCAT (u1.Firstname, ' ', u1.Lastname) AS Examiner,
CONCAT (u2.Firstname, ' ', u2.Lastname) AS Supervisor
FROM betyg_essays grade
INNER JOIN betyg_users u1 ON grade.Examiner = u1.UID
INNER JOIN betyg_users u2 ON grade.Supervisor = u2.UID";
Try this
SELECT CONCAT(betyg_users.`Firstname`," ",betyg_users.`Lastname`) AS `examiner_name`,
CONCAT(betyg_users.`Firstname`," ",betyg_users.`Lastname`) AS `superviser_name`
FROM `betyg_essays`
INNER JOIN `betyg_users` ON betyg_essays.`Examiner` = betyg_users.`UID`
INNER JOIN `betyg_users` ON betyg_essays.`Supervisor` = betyg_users.`UID`;
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.