dsafew1231
2011-06-02 01:19 阅读 29
已采纳

选择两个表中的数据

I have two tables: who contains these fields:


gmdc_employee

file_id     company_id  employee_id     employee_name   file_description    file_date      file_name              folder_access        upload_date  confirmation
    1        20111253     20080407         Mariel             file1         2000-01-01  agent_masterlist.xls    documents/20111253/     2011-05-16    Pending
    2        20112301     20864125         Keisha             file2         2000-02-01  auto_lapsed.xls         documents/20112301/     2011-05-16    Pending
   11        20111253     65513215          Rovy              file15        2008-01-01  attendancesheet.xls     documents/20111253/     2011-06-01    Pending

gmdc_user

company_id  company_name    employee_id     employee_name   username    password_clear           password_hash              login_number         last_login
 25014462       NCST          20132248          Danica      ncst            ncst         c7b080d11d6766a27c02591f74b2ea4d        3           2011-05-23 11:25:03
 20112301       EAC           20113318          Emilio      eac             each         933dd8674c563081260867dfa95b5e74        4           2011-05-23 11:52:58
 20111253       DLSU          20080040          John        dlsu            dasma        3c26c824914144d0addf0ceb850ed78a        15          2011-06-01 10:45:32

This is the query I used when I first need to get items in my gmdc_employee tables: the '%$search%' is my name for my search button and '$listname%' is my name for my panel tabs(A-Z list):

select * 
   from gmdc_employee
   where employee_name like '%$search%' 
     AND employee_name like '$listname%'

The problem is in my search button. Before, my search button will only search the employee_name (my code above is the one i used which is working). But the conditions changed unfortunately :( I was asked that aside from searching the employee_name(found in gmdc_employee table) the user may also search for employee_id (which is found in the gmdc_employee) and company_name (which is found in the gmdc_user), but I don't know how to do that :( I haven't learnt from school yet about the joining of two tables, although I learned a way to do that and tried it. Unfortunately it produced wrong results. So may I give an example?

If the user searched for the company name DLSU, the output should look like this:

employee_id     employee_name   title   file_date    status    confirmation
  20080407        Mariel        file1   2000-01-01   Pending     delete //delete is a link
  65513215         Rovy         file15  2008-01-01   Pending     delete

as to what you see,it searches and echoes files under the company name "DLSU" I hope someone could help me here :( I already tried everything but nothing works the way it was suppose to.

This is what I tried to used that produces wrong outputs. :( it echoed all data needed where and did not look in the condition that only the searched company_name info should output:

select 
      a.employee_id,
      a.employee_name, 
      a.file_description,
      a.file_date,
      a.confirmation 
   from 
      gmdc_employee  AS a 
         JOIN gmdc_user AS b 
   WHERE 
          a.employee_name like'%' 
      AND a.employee_id like '%' 
      AND b.company_name like 'DLSU'
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    douou2026 douou2026 2011-06-02 01:43

    A query which should return the data you provided in the Question is:

    SELECT `e`.`employee_id` ,
           `e`.`employee_name` ,
           `e`.`title` ,
           `e`.`file_date` ,
           `e`.`status` ,
           `e`.`confirmation` AS "status"
    FROM `gmdc_employee` `e`
      JOIN `gmdc_user` `u` ON ( `u`.`company_id` = `e`.`company_id` )
    WHERE `u`.`company_name` LIKE "DLSU"
    
    点赞 评论 复制链接分享
  • doulun0651 doulun0651 2011-06-02 02:39

    I've formatted your queries, and it looks like you were very close... you just didn't have the ON condition for your join

    select 
          a.employee_id,
          a.employee_name, 
          a.file_description,
          a.file_date,
          a.confirmation 
       from 
          gmdc_employee  AS a 
             JOIN gmdc_user AS b
                on a.employee_id = b.employee_id
       WHERE 
              ( a.employee_name like'%$search%' 
              OR b.employee_name like '%$search%' )
          AND b.company_name like 'DLSU'
    

    I'm not really sure this is the proper join is by the employee ID or not... you don't show any instances of same "employee" vs "user". With no "ON" condition, its basically going to create a Cartesian join otherwise (or outright error out)... You may also want to logically OR if you are looking for multiple criteria... such as the name found in one field OR the other... if found in both, no problem...

    点赞 评论 复制链接分享

相关推荐