从内部联接查询中获取数据

时间:2011-03-26 18:57:04

标签: php mysql

你好,请帮我解决这个问题,我从不同的表中获取数据我面临的问题是,在表中有类似的colum名称,如员工有,用户也有名称。查询工作完美,但我担心如何将此数据显示为

$data["employee.name"]
$data["user.name"]

这是查询:

SELECT task.employee_id , task.user_id , task.service_id, user.name, 
       user.pic_path , employee.name ,employee.pic_path 
FROM task  
INNER JOIN employee ON employee.pno = task.employee_id  
INNER JOIN user ON user.pno = task.user_id 
INNER JOIN service ON service.service_id = task.service_id ";

2 个答案:

答案 0 :(得分:4)

SELECT user.name AS username, employee.name AS employeename

你明白了。

答案 1 :(得分:2)

有两个步骤:

  1. 您需要为SQL语句中的两列中的至少一列定义列别名:

    SELECT t.employee_id, 
           t.user_id, 
           t.service_id, 
           u.name AS user_name, 
           u.pic_path, 
           e.name AS employee_name,
           e.pic_path 
      FROM TASK t
      JOIN EMPLOYEE e ON e.pno = t.employee_id  
      JOIN USER u ON ur.pno = t.user_id 
      JOIN SERVICE s ON s.service_id = t.service_id
    
  2. 然后,您需要更新PHP逻辑以使用列别名:

    $empname = $data["employee_name"];
    $username = $data["user_name"];