Table join:
|ID|admin|user |data|
|1 |00001|00002|XXXX|
admin(fk) =users.id,user(fk) =users.id.
Table users:
|id |name|pass|type |
|00001|root|1234|admin|
|00002|user|1235|user |
select join.*,users.name as admin,users.name as user from join
left join users on users.id=join.admin
left join users on users.id=join.user
where grrrrrrr
How can I do this?
Original query, I’m trying to run:
SELECT
visits.id,
visits.patient AS patient_id,
visits.doctor AS doctor_id,
visits.date,
visits.time_booked,
visits.time_arrived,
visits.time_start,
visits.time_end,
visits.type_id,
visits.complain,
visits.diagnosis,
visits.note,
visits.stats,
(personal.name WHERE personal.id=visits.patient and personal.role='patient') AS pt_name,
(personal.name WHERE personal.id=visits.doctor and personal.role='doctor') AS dr_name
FROM
visits ,
personal
You have to alias the table
userswith different aliases. Something likeAlso you have to escape the table name
join,usersince they are reserved keywords in MySQL. Try to avoid those names as object names.SQL Fiddle Demo
This will give you:
Update
For your query after you updated your question, you have to do this the same way, like this:
Updated SQL Fiddle Demo