加入三个没有通用ID #MySQL的表

时间:2016-09-10 21:24:18

标签: php mysql join

我有3个表cam_details,cam_category和upload_data如下..

cam_details:

googleVis

cam_category:

+---------+-----------+-----------
| cam_id | category_id| cam_name |
+========+============+===========
| 1      |  1         | CCTV     |
+--------+------------+-----------
| 2      |  1         | CCtv2    |
+--------+------------+===========
| 3      |  2         | cctv3    |
+--------+------------+=========== 
| 4      |  4         | cctv4    |
+--------+------------+===========

upload_data:

+-------------+---------------+
| category_id | category_name |
+=============+===============+
| 1           |  Analog       | 
+-------------+---------------+
| 2           |  Digital      |
+-------------+---------------+
| 3           |  Network      | 
+-------------+---------------+  
| 4           |  Simple       | 
+-------------+---------------+

现在我想获取数组中凸轮的细节并显示结果。我能够获取cam pic和cam_details,但不能获取cam_category名称。 代码如下......

+---------+-----------+
| cam_id  | FILE_NAME |
+========+============+
| 1      |  abc.jpg   | 
+--------+------------+
| 1      |  abc2.jpg  |
+--------+------------+
| 1      |  abc3.jpg  | 
+--------+------------+  
| 2      |  xyz.jpg   | 
+--------+------------+

1 个答案:

答案 0 :(得分:0)

而不是左连接只做加入并使用selec命令首先为cateroy表...

<?php
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($conn,"SELECT cam_details.*, upload_data.FILE_NAME, cam_category.*  FROM `cam_details` 
 JOIN  upload_data on cam_details.cam_id = upload_data.cam_id JOIN cam_category 
     on cam_details.category_id = cam_category.category_id 
    GROUP BY upload_data.cam_id ORDER BY cam_id DESC");
while($row = mysqli_fetch_array($result)) 
{?>