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 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"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程