0

Hi I have a mysql query below

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
))

I receive an error saying that more than one row is returned

Can I ask why/have guidance

Other questions haven't really helped me to be honest

6
  • i gues you mean; SELECT company,type,code FROM location WHERE code in ( SELECT code FROM meter WHERE meter.location_code=location.code AND meter.code NOT IN ( SELECT meter_code FROM reading )) Mysql now expects the result of the first subquery to be true or false, not a collection of codes. Commented Aug 11, 2017 at 13:13
  • can there be more than 1 meter at a location? Commented Aug 11, 2017 at 13:14
  • @xqbert Nope. I'm reading that it should ideally be INNER JOIN on other answers but they all seem so messy Commented Aug 11, 2017 at 13:15
  • @Dennisvdh nope - the end result is that its meant to bring out a list of meters that haven't got a reading, that unfortunately gives out no result Commented Aug 11, 2017 at 13:16
  • 1
    More than one row is returned. It creates an error. Commented Aug 11, 2017 at 13:30

2 Answers 2

1

Since you don't need data from meter or meter code

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
             )

or to keep with the theme of using exists (this avoids performance impact of distinct and avoids the joins; but a not exists can be slow.

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

             )

This is how it could be done with joins, but the distinct and joins seem like they could be costly. distinct is necessary as I assume a location may have many meters or vice versa or a meter may have many readings which cause the data to be multiplied thus distinct; but at a cost.

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

Would need to test each to find what performance best suits your need. record count in each table indexes and data distribution could all change the performance of each of these. I'm partial to the last one, then first one from a maintenance standpoint but it could be the worst in performance.

Sign up to request clarification or add additional context in comments.

1 Comment

Stuck with this for a week!! Saviour. Thank you.
0

I think you need to use the EXISTS operator:

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))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.