查询来自多个表的数据

时间:2016-09-10 23:11:21

标签: mysql

确定。我认为,我引用了THIS与我的情况类似的其他问题。但我会补充我的情况以及到目前为止我所尝试的内容。

Ticket Table    
*-------------------------------*
|t_id | repair_id | customer_id |
| 1   |     10    |     11      | 
| 2   |   11,12   |     12      | 
| 3   |   13,14   |     13      |
| 4   |   15,16   |     14      |
*-------------------------------* 
Repair Table    
*-----------------------------------------------------*
|repair_id  | ticket_id | device_type | repair_status | 
|     10    |    1      |   laptop    |     open      |
|     11    |    2      |   tablet    |     open      |
|     12    |    2      |   phone     |     open      | 
|     13    |    3      |   phone     |     open      |
|     14    |    3      |   tablet    |     open      |
|     15    |    4      |   laptop    |     open      |
|     16    |    4      |   laptop    |     open      |
*-----------------------------------------------------*
Customer Table    
*-----------------------------*
|customer_id | f_name | l_name | 
|     11     |  John  | Hughes |  
|     12     |  Julia | Brown  |   
|     13     |  Tim   | Duncan |  
|     14     |  Tony  | Price  |  
*-----------------------------*

我希望结果表看起来像这样:

Results Table    
*----------------------------------------------------------*
|customer_name | Repair_# | device_type    | repair_status |
| John Hughes  |   10     | laptop         |    open       | 
| Julia Brown  |   11,12  | tablet,phone   |    open       | 
| Tim Duncan   |   13,14  | phone, tablet  |    open       | 
| Tony Price   |   15,16  | laptop, laptop |    open       | 
*----------------------------------------------------------* 

这是我尝试的查询:

$query  = "SELECT customers.id, customers.f_name, customers.l_name, device_repairs.repair_id, device_repairs.ticket_id, ";
$query .= "device_repairs.device_type,device_repairs.repair_type, device_repairs.ticket_status, ";
$query .= "FROM tickets ";
$query .= "LEFT JOIN customers ON tickets.customer_id = customers.id ORDER BY device_repairs.ticket_id DESC";

我知道我的查询不对,但我不太确定如何设置INNER JOIN。

REVISION

所以经过一些审查。我已经添加了修改后的表格结构。

Ticket Table    
*--------------------------------------------------------------------*
|t_id|repair_id|cust_id|cust_fname|cust_lname|device_type|repair_stat|
| 1  |   10    |  11   |  John    | Hughes   | Laptop    |   open    |
| 2  |   11    |  12   |  Julia   | Brown    | Tablet    |   open    |
| 2  |   12    |  12   |  Julia   | Brown    | Phone     |   open    | 
*--------------------------------------------------------------------* 
Repair Table    
*-----------------------------------------------------*
|repair_id  | ticket_id | device_type | repair_status | 
|     10    |    1      |   laptop    |     open      |
|     11    |    2      |   tablet    |     open      |
|     12    |    2      |   phone     |     open      | 
|     13    |    3      |   phone     |     open      |
|     14    |    3      |   tablet    |     open      |
|     15    |    4      |   laptop    |     open      |
|     16    |    4      |   laptop    |     open      |
*-----------------------------------------------------*
Customer Table    
*-----------------------------*
|customer_id | f_name | l_name | 
|     11     |  John  | Hughes |  
|     12     |  Julia | Brown  |   
|     13     |  Tim   | Duncan |  
|     14     |  Tony  | Price  |  
*-----------------------------*

并且修订结果表看起来像这样:

Results Table    
*----------------------------------------------------------*
|customer_name | Repair_# | device_type    | repair_status |
| John Hughes  |   10     | laptop         |    open       | 
| Julia Brown  |   11     | tablet         |    open       |
| Julia Brown  |   12  |  | phone          |    open       |  
*----------------------------------------------------------* 

希望这有用并且更有意义。

我创建了这个查询,以便结果表输出正确。感谢您的所有投入。

$sql = "SELECT customers.id, customers.f_name, customers.l_name, device_repairs.repair_id, device_repairs.device_type, ";
$sql .="device_repairs.repair_type, device_repairs.ticket_status, device_repairs.ticketopen ";
$sql .="FROM customers, device_repairs WHERE customers.id = device_repairs.customer_id ORDER BY device_repairs.ticketopen DESC";

1 个答案:

答案 0 :(得分:0)

票表设计不佳。我不会在一个字段中放置两个修复ID。拥有ID(主键)和外键的整个想法都将丢失。

您的票证表应该有7个而不是4个,其中的修复ID应该只有一个值。此外,repair_id应该被描述为外键并链接到Repair表。

在此之后,您可以在customer_id上将customer表连接到ticket表,并进一步加入ticket表上的修复表。