简体   繁体   中英

MySQL ORDER BY combined with LIMIT 1 returns empty result

We do have an script that automatically inserts a hash value out of a pool into an article if it does meet certain conditions. The extension doing that was programmed several years ago and worked well so far. Now it stopped working and nobody knows why. After a long time of debugging I found the error in the SQL statement.

The generated statement looked like this:

SELECT `hash` 
FROM `table` 
WHERE `is_used` =0
ORDER BY `uid`
LIMIT 1

If I just remove the "ORDER BY uid ", it works again. I just would like to know why this happened out of the blue. The hoster said that there were no changes/updates made on the systems side and I checked all related files, none of those were changed within the last year.

About the system. TYPO3 4.5.2 PHP Version 5.3.8-1~dotdeb.1 MySQL Version 5.1.57

Thanks in advance

Edit: Sorry for the confusion. With "stopped working" I meant that from one day to another no hashes were inserted into the articles. When I execute the statement with the ORDER BY, I'm getting one empty row as a result. The uid column does still exists. There were no changes to anything on the system, including the database and its tables. I've attached an image with the table structure and both query results on the right (well, it's not really attached, since I need more reputation to attach it).

To clarify my question again: The query worked before and returned a hash. Now it only works when I remove the ORDER BY and I would like to know how this could happen, since I'm curious and it may help me with future errors. http://i.stack.imgur.com/Dpip4.png

PS: I've edited the table name, don't be scared ;)

Looking at your screen print, whether you have the order by clause or not a row is being returned.

You are using LIMIT 1 to only bring back a single record. When you have the order by clause in place you bring back the record with the lowest uid. It just happens that this has a blank hash value (possibly set up in error at some point).

I would be tempted to just check that the hash is not blank in the WHERE clause.

SELECT `hash` 
FROM `table` 
WHERE `is_used` =0
AND `hash` != ''
ORDER BY `uid`
LIMIT 1

I'm feeling so silly right now. Of course it was an easy answer and the system "didn't just stop working". @Kickstart was right, there was an empty row in the table. I didn't check the table correctly. When I opened it up in PhpMyAdmin, for some reason it wasn't ordered by uid (the others are ordered by uid by default). I clicked on the uid field and promptly saw the empty row. http://i.stack.imgur.com/Vpco7.png

Thank you all for your help and showing me that I'm not crazy, but just blind ;)

I'm going to take a look at the import script to ignore empty fields by default. Never trust a system you didn't write yourself^^

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM