【发布时间】:2017-03-10 23:35:54
【问题描述】:
我们有三个表,“cameras”、“cameratypes”和“locations”。
这里每个都有样本数据。
位置(location_id、user_id、contact1)
select * from locations;
+-------------+---------+--------------------+
| location_id | user_id | contact1 |
+-------------+---------+--------------------+
| 38 | 96 | Joe Blow |
| 42 | 96 | Joe Blow |
| 45 | 105 | Francis McLucky |
| 50 | 113 | Ogre L'Bosch |
| 53 | 113 | Ogre L'Bosch |
+-------------+---------+--------------------+
相机类型(相机类型 ID,标题)
select * from cameratypes;
+---------------+----------------+
| cameratype_id | title |
+---------------+----------------+
| 1 | Pan Tilt Zoom |
| 2 | Fixed |
| 3 | Mobile |
+---------------+----------------+
相机(camera_id, cameratype_id, location_id, user_id)
select * from cameras
+-----------+---------------+-------------+---------+
| camera_id | cameratype_id | location_id | user_id |
+-----------+---------------+-------------+---------+
| 4633 | 3 | 38 | 96 |
| 4637 | 3 | 38 | 96 |
| 4644 | 1 | 38 | 96 |
| 4962 | 1 | 45 | 105 |
| 4667 | 2 | 45 | 105 |
| 4681 | 2 | 50 | 113 |
| 4689 | 2 | 50 | 113 |
| 4684 | 2 | 50 | 113 |
| 4682 | 3 | 50 | 113 |
| 4691 | 2 | 53 | 113 |
+-----------+---------------+-------------+---------+
我正在尝试将表格加入报告中,其中输出是摄像机列表,带有所有者的姓名和摄像机类型。
然而,当我运行查询时,我得到了这个结果(基本上是将用户 113 的摄像头数量(他有五个)乘以他拥有的摄像头位置数量(即两个)。
我的查询是:
SELECT
loc.contact1 AS 'Camera Owner Name',
loc.location_id AS 'Location ID',
cam.camera_id AS 'Camera ID',
camtype.title AS 'Camera Type'
FROM engine4_securonet_cameras AS cam
LEFT JOIN engine4_securonet_locations AS loc ON cam.user_id = loc.user_id
JOIN engine4_securonet_cameratypes AS camtype ON cam.cameratype_id = camtype.cameratype_id
WHERE loc.user_id = 113
ORDER BY cam.camera_id;
查询的结果是:
+-------------------+-------------+-----------+--------+
| Camera Owner Name | Location ID | Camera ID | Type |
+-------------------+-------------+-----------+--------+
| Alexis Neve | 50 | 4681 | Fixed |
| Alexis Neve | 53 | 4681 | Fixed |
| Alexis Neve | 50 | 4682 | Mobile |
| Alexis Neve | 53 | 4682 | Mobile |
| Alexis Neve | 50 | 4684 | Fixed |
| Alexis Neve | 53 | 4684 | Fixed |
| Alexis Neve | 50 | 4689 | Fixed |
| Alexis Neve | 53 | 4689 | Fixed |
| Alexis Neve | 50 | 4691 | Fixed |
| Alexis Neve | 53 | 4691 | Fixed |
+-------------------+-------------+-----------+--------+
10 rows in set (0.00 sec)
如何消除这种重复?非常感谢!
【问题讨论】:
-
我看不到任何重复。期望的结果是什么样的?