Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.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
MySQL查询“多行”_Mysql - Fatal编程技术网

MySQL查询“多行”

MySQL查询“多行”,mysql,Mysql,嗨,我在下面有一个mysql查询 SELECT `company`,`type`,`code` FROM `location` WHERE ( SELECT code FROM meter WHERE `meter`.`location_code`=location.code AND meter.code NOT IN ( SELECT meter_code FROM reading )) 我收到一个错误,说返回了多行 我能问一下为什么/有指导吗 老实说,其他问题并没有真正帮助我我认为您需要使

嗨,我在下面有一个mysql查询

SELECT `company`,`type`,`code` FROM `location` WHERE (
SELECT code FROM meter WHERE `meter`.`location_code`=location.code AND meter.code NOT IN (
SELECT meter_code FROM reading
))
我收到一个错误,说返回了多行

我能问一下为什么/有指导吗


老实说,其他问题并没有真正帮助我

我认为您需要使用EXISTS操作符:

SELECT `company`,`type`,`code` 
FROM `location` 
WHERE EXISTS (SELECT code 
              FROM meter 
              WHERE `meter`.`location_code` = location.code AND 
                     meter.code NOT IN (SELECT meter_code FROM reading))

因为您不需要仪表或仪表代码中的数据

SELECT `company`,`type`,`code` 
FROM `location` 
WHERE EXISTS (SELECT * 
              FROM meter 
              LEFT JOIN Reading R
                on meter.code = R.meter_Code
              WHERE `meter`.`location_code`=location.code 
                and R.meter_Code is null
             )
或者为了与使用存在的主题保持一致,这避免了distinct的性能影响,避免了连接;但是一个不存在的过程可能很慢

SELECT `company`,`type`,`code` 
FROM location l 
WHERE EXISTS (SELECT * 
              FROM meter m
              WHERE m.location_code=l.code 
                and not exists (SELECT * 
                                FROM READING r
                                WHERE R.meter_Code = m.Code

             )
这就是连接的实现方式,但是distinct和join似乎代价高昂。由于我假设一个位置可能有许多米,或者相反,或者一个米可能有许多读数,导致数据相乘,因此不同,所以不同是必要的;但要付出代价

SELECT DISTINCT l.company,l.type,l.code
FROM location l
INNER JOIN METER M
 on l.code = m.location_Code
LEFT JOIN reading R
 on R.Meter_Code = m.Code
WHERE r.meter_Code is null

需要测试每一个,以找到最适合您需要的性能。每个表中的记录计数、索引和数据分布都可能改变每个表的性能。从维护的角度来看,我偏爱最后一个,然后是第一个,但它的性能可能是最差的。

I gues,你是说;从位置选择公司、类型、代码在从仪表中选择代码在仪表中选择代码。位置\代码=位置。代码和仪表。代码不在从Mysql读取的选择仪表\代码中现在期望第一个子查询的结果为真或假,而不是一组代码。一个位置是否有超过1个仪表?@xqbert Nope。我读到,理想情况下,应该是在其他答案上进行内部连接,但它们似乎都是这样messy@Dennisvdh不-最终的结果是,它意味着显示一个没有读数的仪表列表,不幸的是,没有给出任何结果。返回的行数超过一行。它产生了一个错误。坚持了一周!!救世主。非常感谢。