Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从多个关系表查询数据_Php_Mysql_Codeigniter_Mysqli - Fatal编程技术网

Php 从多个关系表查询数据

Php 从多个关系表查询数据,php,mysql,codeigniter,mysqli,Php,Mysql,Codeigniter,Mysqli,我试图在一个查询中查询多个关系表中的数据。我有一个收据表,通过将用户id和客户id存储为FK来保存客户购买的信息。我想在获取用户名和客户名的同时查询整个收据列表。这可以/应该在单个查询中完成吗 Receipt Table *--------------------------------------------------* |receipt_id | user_id | customer_id | receipt_info | | 1 | 1 | 1

我试图在一个查询中查询多个关系表中的数据。我有一个收据表,通过将用户id和客户id存储为FK来保存客户购买的信息。我想在获取用户名和客户名的同时查询整个收据列表。这可以/应该在单个查询中完成吗

Receipt Table    
*--------------------------------------------------*
|receipt_id | user_id | customer_id | receipt_info |
|     1     |    1    |     1       | 'Some text'  | 
|     2     |    2    |     1       | 'Some text'  | 
|     3     |    2    |     1       | 'Some text'  | 
|     4     |    3    |     2       | 'Some text'  | 
|     5     |    3    |     3       | 'Some text'  |
*--------------------------------------------------* 
User Table    
*-----------------------*
|user_id    | user_name | 
|     1     |   Michael |    
|     2     |   Dwight  |   
|     3     |   Jim     |   
|     4     |   Andy    | 
|     5     |   Stanley |  
*-----------------------*
Customer Table    
*---------------------------*
|customer_id| customer_name | 
|     1     |   Schofield   |    
|     2     |   Julia       |   
|     3     |   Dunmore High|   
|     4     |   Deckert     | 
|     5     |   Prince Paper|  
*---------------------------*
所以我希望我的结果集是这样的:

Results Table    
*------------------------------------------------------*
|receipt_id | user_name | customer_name | receipt_info |
|     1     |  Michael  | Schofield     | 'Some text'  | 
|     2     |  Dwight   | Schofield     | 'Some text'  | 
|     3     |  Dwight   | Schofield     | 'Some text'  | 
|     4     |  Jim      | Julia         | 'Some text'  | 
|     5     |  Jim      | Dunmore High  | 'Some text'  |
*------------------------------------------------------* 
您可以做的是:

SELECT receipt_id,user_name,customer_name, receipt_info

FROM user u
INNER JOIN receipt r
on r.user_id = u.user_id
INNER JOIN customer c
on c.customer_id = r.customer_id
我想这就是你想要的….

你能做的是:

SELECT receipt_id,user_name,customer_name, receipt_info

FROM user u
INNER JOIN receipt r
on r.user_id = u.user_id
INNER JOIN customer c
on c.customer_id = r.customer_id

我想这就是你想要的……

你应该使用连接。有关更多信息,请检查此链接您应该使用连接。有关更多信息,请查看此链接